Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is updating an Array working in scala?

Tags:

scala

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?

like image 579
Karel Bílek Avatar asked Dec 15 '22 21:12

Karel Bílek


1 Answers

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.

like image 111
Didier Dupont Avatar answered Dec 30 '22 19:12

Didier Dupont