Scala pattern matching with lowercase variable name

This is not specific to patterns with alternatives, and it is not a bug. An identifier that begins with a lowercase letter in a pattern represents a new variable that will be bound if the pattern matches.

So, your example is equivalent to writing:

x match {
   case MyValue1 | MyValue2 => println ("first match")
   case y | z => println ("second match")
}

You can work around this by using backticks:

x match {
   case MyValue1 | MyValue2 => println ("first match")
   case `myValue1` | `myValue2` => println ("second match")
}

Leave a Comment