我试图通过元组赋值声明变量,例如:
val (hi,bye) = ("hi","bye")
显然这很好.但是,当第一个字符为大写时,编译器会爆炸并抱怨这些变量尚未定义.
val (Hi,Bye) = ("hi","bye")
为什么这不起作用?我正在运行Scala 2.11.
顺便说一句(正如预期的那样):
val Hi = "hi" val Bye = "bye"
解决方法
从
here开始:
The tuple on the left-hand side is a pattern; names starting with capital letters are treated as constants when occurring inside a pattern. These constants must exist as values in the context. You find the exact semantics in the Scala spec under pattern matching.