tags:

views:

84

answers:

1

Where exactly are the *=/+=/etc methods declared for the subclasses of AnyVal? I assume something special is done for these types because as a val those are invalid but as a var they are fine. Is this just yet more syntatic sugar ? I assume it is turning

a *= 5

into

a = a * 5

which obviously fails for a val. Is this intuition correct? I also assume it only attempts this for AnyVals?

Thanks :)

+5  A: 

Your intuition is correct. For any class, not only the subclasses of AnyVal, if an assignment method does not exists, then a OP= b is turned into a = a OP b. Mind you, OP, in this case, must be non-alphanumeric characters.

Daniel
And *a* must be mutable, i.e., a **var**.
Randall Schulz
So in a related situation, then for a var `a` which has defined both the say, `OP=` and `OP` methods, does `a OP= b` become `a = a OP b` or `a.OP=(b)` ?
Jackson Davis
And to answer my own question, it appears that `OP=` takes precedence (which makes sense) and `OP` is only called if `OP=` doesnt exist.
Jackson Davis