this works as expected
scala> 3 match { case x:Int => 2*x }
res1: Int = 6
why does this fail?
scala> 3 match { case $x:Int => 2*$x }
:1: error: '=>' expected but ':' found.
3 match { case $x:Int => 2*$x }
^
scala> 3 match { case `$x`:Int => 2*$x }
:1: error: '=>' expected but ':' found.
3 match { case `$x`:Int => 2*$x }
^
scala> 3 match { case `$x` : Int => 2*$x }
:1: error: '=>' expected but ':' found.
3 match { case `$x` : Int => 2*$x }
‘$’ is supposed to be a valid identifier character, as demonstrated here:
scala> var y = 1 y: Int = 1 scala> var $y = 2 $y: Int = 2
Thanks
Actually, while all other answers are correct in some sense, the explanation here is simpler. A dollar sign is considered an uppercase letter according to the specification and, thus, is treated like a constant in a pattern match.