4

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?

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.