views:

72

answers:

1

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 and 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
+11  A: 

You either have to capitalize the vals or you have to put the identifiers into backticks like this:

  case (`b`, `c`) => 200
Moritz
Aha! That rings a bell.
Synesso