Why array(index) = value is equivalent of array.update(index, value)?

Hi there, i’m just starting reading Programming in scala and in chapter 3 it raised me a (probably dumb) question.
If i have this code:

val greetings = new Array[String](1)
greetings(0) = "teste"

This will be equivalent to:

val greetings = new Array[String](1)
greetings.update(0, "teste")

But, if greeting(0) will call greeting.apply(0) and apply method will return a String, so why the compiler are not looking to some kind of attribution in the string returned from greeting(0)? I think this would fail in this case, but, why the first example above works? It’s just a matter of “compiler knows” or it is a method from the Arrays Trait?

Thanks in advance.

The compiler knows, the specs says that if you have foo(x) = y then the compiler will check if the type of foo has a method called update that accepts two values of the types of x and y.


You can’t have a method called = or anything like that, so that line cannot be interpreted as greetings.apply(0).=("teste"). So that means the compiler can unambiguously recognize that syntax as a call to greetings.update.