0
votes

If we go by the definition in "Programming in Scala" book:

When you apply parentheses surrounding one or more values to a variable, Scala will transform the code into an invocation of a method named apply on that variable

Then what about accessing the elements of an array? eg: x(0) is transformed to x.apply(0) ? (let's assume that x is an array). I tried to execute the above line. It was throwing error. I also tried x.get(0) which was also throwing error.

Can anyone please help?

3
what error are you getting? compile or runtime error? does it work if you just do x(0)? x.apply(0) is the correct way to access an array, although usually you would use the x(0) syntax instead of writing out apply explicitly.puhlen
x(0) is the same as x.apply(0) and should work on an Array. What error were you getting? Array has no get() method. Maps and Options have get() methods.jwvh
sorry, while I intended to ask the question like below.. I am running my program as below.. scala> val x = Array(1,2,3) x: Array[Int] = Array(1, 2, 3) scala> x(1)=200 scala> x.apply(1)=200 <console>:13: error: missing argument list for method apply in class Array Unapplied methods are only converted to functions when a function type is expected. You can make this conversion explicit by writing apply _ or apply(_) instead of apply. x.apply(1)=200 ^user3103957
above, when I replace x(1) with x.apply(), it throws error. I am unable to edit the code in the comment section.user3103957
"it was throwing error" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?Jörg W Mittag

3 Answers

2
votes

() implies apply(),

Array example,

scala> val data = Array(1, 1, 2, 3, 5, 8)
data: Array[Int] = Array(1, 1, 2, 3, 5, 8)

scala> data.apply(0)
res0: Int = 1

scala> data(0)
res1: Int = 1

not releated but alternative is to use safer method which is lift

scala> data.lift(0)
res4: Option[Int] = Some(1)

scala> data.lift(100)
res5: Option[Int] = None

**Note: ** scala.Array can be mutated,

scala> data(0) = 100

scala> data
res7: Array[Int] = Array(100, 1, 2, 3, 5, 8)

In this you can not use apply, think of apply as a getter not mutator,

scala> data.apply(0) = 100
<console>:13: error: missing argument list for method apply in class Array
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `apply _` or `apply(_)` instead of `apply`.
       data.apply(0) = 100
            ^

You better use .update if you want to mutate,

scala> data.update(0, 200)

scala> data
res11: Array[Int] = Array(200, 1, 2, 3, 5, 8)

User defined apply method,

scala>   object Test {
     | 
     |     case class User(name: String, password: String)
     | 
     |     object User {
     |       def apply(): User = User("updupd", "password")
     |     }
     | 
     |   }
defined object Test

scala> Test.User()
res2: Test.User = User(updupd,password)
1
votes

If you add an apply method to an object, you can apply that object (like you can apply functions).

The way to do that it is just apply the object as if it was a function, directly with (), without a "dot".

val array:Array[Int] = Array(1,2,3,4)

array(0) == array.apply(0)
1
votes

For

x(1)=200

which you mention in the comment, the answer is different. It also gets translated to a method call, but not to apply; instead it's

x.update(1, 200)

Just like apply, this will work with any type which defines a suitable update method.