() 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)
x(0)is the same asx.apply(0)and should work on anArray. What error were you getting?Arrayhas noget()method. Maps and Options haveget()methods.apply _orapply(_)instead ofapply. x.apply(1)=200 ^