val A = 3
val (A) = (3)
Both correct. But:
val (A,B) = (2,3)
can’t be compiled:
scala> val (A,B) = (2,3)
<console>:7: error: not found: value A
val (A,B) = (2,3)
^
<console>:7: error: not found: value B
val (A,B) = (2,3)
^
Why?
In the second code snippet, it using pattern matching to do assessment.
It is translated to the follow code:
When Scala is doing pattern matching, variable starts with a upper case in the pattern is considered as an constant value (or singleton Object), so
val (a, b) = (2, 3)works but notval (A, B) = (2, 3).BTW, your first code snippet does not using pattern matching, it’s just an ordinary variable assignment.
If you using
Tuple1explicitly, it will have same error.Here is some interesting example: