Consider the following two declarations:
var x: Int = 0
val y: Int = 0
x
is a variable whereas y
is a value, both of the same type: Int
.
I can use +=
on x
:
x += 1
x.+=(1)
However, if I try to use +=
on y
, I get a compile-time error — "Not Found Error" (E008
) — with the description:
+=
is not a member ofInt
I understand that y
is immutable because it's a value, and this behavior is desirable.
What mechanism does Scala use to provide x
and y
with different methods taking into account that they are both of type Int
?