Question

I can get the index = 2 item from a List using apply.

scala> List(1,2,3).apply(2)
res3: Int = 3

scala> val x = List(1,2,3)
x: List[Int] = List(1, 2, 3)

scala> x(2)
res4: Int = 3

scala> List(1,2,3).apply(2)
res5: Int = 3

But, why can't I do the following?

scala> List(1,2,3).(2)
<console>:1: error: identifier expected but '(' found.
       List(1,2,3).(2)
                   ^
Was it helpful?

Solution

you don't need .

scala> List(1,2,3)(2)
res1: Int = 3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top