Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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
like image 506
Synesso Avatar asked Dec 21 '22 23:12

Synesso


1 Answers

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

  case (`b`, `c`) => 200
like image 163
Moritz Avatar answered Dec 25 '22 23:12

Moritz