How is an array running in scala updated?

I am sometimes puzzled by the random syntactic magic of scala.

I thought to write

array(5)

is just a shortcut to

array.apply(5) . (As written in the documentation for the array.)

However i can do pretty happily

array(5) = 3

But I can not do

array.apply(5) = 3 .

What's happening?

+4
source share
1 answer

On the left side of = there are different rules: ax = b translated into a.x_=(b) (provided that there is also a method x ()) a(i1,... in) = b converted to a.update(i1...,in, b)

So array(5) = 3 is array.update(5,3)

Of course, for arrays, it compiles directly to the write array without calling a method between them.

+12
source

All Articles