How to reference a val in a case statement?
I'm having a slow morning. I thought referencing an existing val in a case statement would be OK. But it seems it is interpreted as a local variable definition. A rudimentary googling didn't help an开发者_JAVA技巧d I don't have my staircase book with me.
In the following, what is the syntax that would allow me to match on case (b,c)?
scala> val (a,b,c) = (1,2,3)
a: Int = 1
b: Int = 2
c: Int = 3
scala> (2,3) match {
| case (a,b) => 100
| case (b,c) => 200
| case _ => 999
| }
<console>:8: error: unreachable code
case (b,c) => 200
You either have to capitalize the val
s or you have to put the identifiers into backticks like this:
case (`b`, `c`) => 200
精彩评论