TraderJoeChicago
TraderJoeChicago

Reputation: 6315

What is the rule for parenthesis in Scala method invocation?

Isn't toList a method that converts something into a List?

If yes so why can't I use parenthesis with it? I must be missing something more fundamental here.

Here is the example:

val l = Array(1,2,3).toList // works fine

val l = Array(1,2,3).toList() // gives the error below

Not enough arguments for method apply: (n: Int)Int in trait LinearSeqOptimized. Unspecified value parameter n.

Upvotes: 57

Views: 16133

Answers (4)

Ricardo
Ricardo

Reputation: 4308

11 Years later... The exception Not enough arguments for method apply: (n: Int)Int in trait LinearSeqOptimized message gives you the clue. Checking the docs for LinearSeqOps.html#apply(n:Int):A:

def apply(n: Int): A Get the element at the specified index.

The toList in the Array doesn't take any argument [see Array:toList docs] and converts it to the l: List[Int], which is a LinearSeqOps trait:

val l = Array(1,2,3).toList        // l: List[Int] = List(1, 2, 3)

There are many ways to call apply(0) in the list l to get the element at index 0. They all do the same thing:

var el1 = l.apply(0)               // el1: Int = 1
el1 = l(0)                         // el1: Int = 1
el1 = Array(1,2,3).toList.apply(0) // el1: Int = 1
el1 = Array(1,2,3).toList(0)       // el1: Int = 1

But if we don't provide the Int argument to apply(n: Int), the code will fail with the given error. The same variations will all give the same error:

el1 = Array(1,2,3).toList()        // not enough arguments for method apply: (n: Int)
el1 = Array(1,2,3).toList.apply()  // not enough arguments for method apply: (n: Int)
el1 = l.apply()                    // not enough arguments for method apply: (n: Int)
el1 = l()                          // not enough arguments for method apply: (n: Int)

Upvotes: 0

Vishal John
Vishal John

Reputation: 4382

Let me answer from Scala coding style perspective.

Scala style guide says...

Omit empty parenthesis, only be used when the method in question has no side-effects (purely-functional). In other words, it would be acceptable to omit parentheses when calling queue.size, but not when calling println().

Religiously observing this convention will dramatically improve code readability and will make it much easier to understand at a glance the most basic operation of any given method. Resist the urge to omit parentheses simply to save two characters!

Upvotes: 5

Knut Arne Vedaa
Knut Arne Vedaa

Reputation: 15742

Your second line is actually interpreted as

val l = Array(1,2,3).toList.apply()

since foo(x) is "magic" syntax for foo.apply(x).

That's why the complier complains about "not enough arguments", as the apply method on lists takes one argument.

Thus you can write e.g.:

scala> val i = Array(1, 2, 3).toList(1)
i: Int = 2

Upvotes: 21

Rex Kerr
Rex Kerr

Reputation: 167891

If a method is defined as

def toList = { /* something */ }

then it must be called as

object.toList

with no extra parentheses. We say that this method has zero parameter lists.

We could also define a parameter list but put nothing in it:

def toList() = { /* something */ }

Now, we could call either of

object.toList()
object.toList

since Scala allows the shortcut of omitting parentheses on method calls.

As far as the JVM is concerned, there is no difference between the first definition ("zero parameter lists") and the second ("one empty parameter list"). But Scala maintains a distinction. Whether this is a good idea or not is debatable, but the motivation might be clearer when you realize that we can also

def toList()() = { /* something */ }

which is known as two empty parameter lists, and then call any of

object.toList()()
object.toList()
object.toList

and now, if we were to convert this into a function, we would type it as

() => () => T   /* T is the return value of the something */

while the second definition would be

() => T

which is clearly different conceptually, even if practically you use it the same way (put in nothing and sooner or later get out a T).

Anyway, toList doesn't need any parameters, and the Scala standard is to leave off the parens unless the method changes the object itself (rather than just returning something), so it's def toList without any parens afterwards. And thus you can only call it as object.toList.

Upvotes: 72

Related Questions