开发者

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 vals or you have to put the identifiers into backticks like this:

  case (`b`, `c`) => 200
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜