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 🙂
Your intuition is correct. For any class, not only the subclasses of
AnyVal, if an assignment method does not exists, thena OP= bis turned intoa = a OP b. Mind you,OP, in this case, must be non-alphanumeric characters.This is described on the Scala Reference (the Scala specification), section 6.12.4.