I’ve noticed the following behavior in scala when trying to unwrap tuples into vals:
scala> val (A, B, C) = (1, 2, 3)
<console>:5: error: not found: value A
val (A, B, C) = (1, 2, 3)
^
<console>:5: error: not found: value B
val (A, B, C) = (1, 2, 3)
^
<console>:5: error: not found: value C
val (A, B, C) = (1, 2, 3)
^
scala> val (u, v, w) = (1, 2, 3)
u: Int = 1
v: Int = 2
w: Int = 3
Is that because scala’s pattern matching mechanism automatically presumes that all identifiers starting with capitals within patterns are constants, or is that due to some other reason?
Thanks!
Yes, and it gets worse:
The above will compile and fail at runtime with a
ClassCastException. It is easy to forget that the(i, j)declaration is a pattern.EDIT: for ziggystar, the Scala assignment rules state that in the statement:
pcan be either an identifier or a pattern (see section 15.7 of Programming in Scala, pp284). So for example, the following is valid:Taking this together with the fact that patterns are erased (i.e. parametric type information is unchecked) means that my original example will compile.