I can assign a tuple as follows:
var (min, max) = (1, 2)
But I cannot then re-assign as follows
(min, max) = (1, 3) //compiler error: ';' expected but '=' found
Instead I seem to have to do:
min = 1
max = 3
Why does the latter work whereas the former does not?
Well, because it was spec’ed that way, I suppose.
This, the tuple assignment, is an example of pattern matching. Pattern matching happens in three places that I recall of:
So all these cases work:
Now, take the last example, the one using
case. Note thatnandcin that example are not the samenandcdefined a bit earlier. The pattern match will assign values to new identifiersnandc, which will shadow the previous definition for the escope of thecasestatement. The same thing happened on theforexample, which did not changenandcpreviously defined.Now, what you want to happen is to overwrite the previous value, instead of assign new values to new identifiers. That’s not how pattern matching works, which means making it happen would entail an entirely new rule. Since Scala gently prods people towards immutability, I suppose it’s not unreasonable they did not create a new rule just to handle this.