Apply () in Scala giving a compilation error?

Hi, I have the following code snippet:

var z:Array[String] = new Array[String](4);
z(0) = "Dave"; 
z(1) = "Joe";
z(2) = "Jim";
z.apply(3) = "Roger";

The last line shows a compile-time error: "Missing method arguments are applied in the Array class, follow this method with" _ "if you want to treat it as a partially applied function"

This doesn't make sense to me, as I read that when you use the parentheses associated with yet another variable value, Scala converts the code into a method call called apply for that variable. Therefore, if the following line:

z(2) = "Jim";

converted to

z.apply(2) = "Jim";

Then why line

z.apply(3) = "Roger";

give me a compile time error?

I am new to Scala, so any help would be greatly appreciated!

+4
2

:

z(2) = "Jim";

z.update(2, "Jim")

apply , .

. : scala -print val ar = Array(1, 2, 3)

, ar(2) = 5, . ( ), ( ) :

    $line3.iw.ar().update(2, 5);
+7

Scala - 6.15 :

f(args) = e = f.update(args, e), update, f.

, z

z(i) = "Name" z.update(i, "Name")

z.apply(3) = "Roger" , .


:

assignment      expansion

x.f = e         x.f_=(e)
x.f() = e       x.f.update(e)
x.f(i) = e      x.f.update(i, e)
x.f(i, j) = e   x.f.update(i, j, e)
+2

Source: https://habr.com/ru/post/1610203/


All Articles