Scala enumeration type fail in match/case

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.

Answers


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

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.


a in your case statement is an unbound variable which matches anything. You need to explicitly check the value is equal to a:

x match {
    case v if v == a => "a"
    case _ => "b"
}

Need Your Help

Adding placeholder text to textbox

c# wpf placeholder

I am looking for a way to add placeholder text to a textbox like you can with a textbox in html5.

Using up and down button to scroll the page banner upwards or downwards

php javascript html scroll

I placed two buttons at the header 'up-arrow' and 'down arrow'. At the content area I placed multiple table which contain the banner image. Is it possible if I click on the upwards button the 1st t...