The a
and b
in your match
block aren't the same as the enumerated values a
and b
, the pattern matcher will simply match x
in the first case and bind it to a new value a
(the second case will be ignored)
To avoid this, you have two choices:
1) wrap the values in backticks:
x match {
case `a` => "a"
case `b` => "b"
}
2) Make the enumerated values uppercase:
object EnumType extends Enumeration {
type EnumType = Value
val A, B = Value
}
import EnumType._
val x: EnumType = B
x match {
case A => "a"
case B => "b"
}
Given that these values are (to all intents an purposes) constants, using uppercase is the more common/idiomatic solution.
Note that only the initial letter needs to be uppercase, not the name of the entire literal.