Scala enumeration type fail in match/case

enumeration, scala

Solution

Like @Kevin Wright and @Lee just said, `a` and `b` work as variable patterns, not as `EnumType` values.

Another option to fix your code is making it explicit you are referencing values from the `EnumType` singleton:

scala> x match { case EnumType.a => "a" case EnumType.b => "b" }
res2: String = b

Problem

Enumerated values seem to fail in match/case expressions. This is what happens in a worksheet. ``` object EnumType extends Enumeration { type EnumType = Value val a, b = Value } import EnumType._ val x: EnumType = b //> x : ... .EnumType.EnumType = b x match { case a => "a" case b => "b" } //> res0: String = a if (x == a) "a" else "b" //> res1: String = b ``` What's going on? Thanks.

Original source