Hey I have the following piece of code:
var z:Array[String] = new Array[String](4);
z(0) = "Dave";
z(1) = "Joe";
z(2) = "Jim";
z.apply(3) = "Roger";
The last line here is giving a compile time error saying - "missing arguments for method apply in class Array; follow this method with `_' if you want to treat it as a partially applied function"
This does not make sense to me, as I have read that when you apply parentheses surrounding one more values to a variable, Scala will transform the code into an invocation of a method named apply on that variable. So if the following line:
z(2) = "Jim";
gets converted to
z.apply(2) = "Jim";
Then why does the line
z.apply(3) = "Roger";
give me a compile time error?
I am new with Scala so any help would be great thanks!
Array#apply
docs state:Indices start at 0; xs.apply(0) is the first element of array xs. Note the indexing syntax xs(i) is a shorthand for xs.apply(i).
– Kevin Meredithupdate
, notapply
. The docs forupdate
are similar:Indices start at 0; xs.update(i, x) replaces the ith element in the array. Note the syntax xs(i) = x is a shorthand for xs.update(i, x).
– Shadowlands