Why doesn't scala allow the $$ identifier in a case statement?
this works as expected
scala> 3 match {case x: Int => 2 * x}
res1: Int = 6
why is it failing?
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}
'$' must be a valid identifier character as shown here:
scala> var y = 1 y: Int = 1 scala> var $ y = 2 $ y: Int = 2
thanks
+2
a source to share
4 answers
Even though it's discouraged, $ can be written to identifiers. But an identifier that starts with $ is not considered a variable identifier - only identifiers that start with lowercase letters. On the other hand, a typed template identifier: Type requires a variable identifier before :. This is why the match is rejected.
+7
a source to share