I am still sometimes puzzled by scala occasional syntactic magic.
I thought, that writing
array(5)
is just a shortcut for
array.apply(5)
. (As is written in the documentation for Array.)
However, I can do quite happily
array(5) = 3
But I cannot do
array.apply(5) = 3
.
What is going on?
There are different rules on the left side of =
:
a.x = b
is translated to a.x_=(b)
(provided there is also an x() method)
a(i1,... in) = b
is transformed into a.update(i1...,in, b)
So array(5) = 3
is array.update(5,3)
Of course, for arrays it is directly compiled to an array write without a method call in between.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With