barbara
barbara

Reputation: 3201

What is the purpose of double method brackets in Scala?

I have a code

object App {
  def main(args: Array[String]) = print {CL().f()()()}
}

case class CL() {
  def f()()() = 1
}

You can see there a method call f()()(). But if I execute f() it returns the same result.

So what is the difference betweenn f()()() and f() in Scala?

Upvotes: 8

Views: 2430

Answers (1)

Ben Reich
Ben Reich

Reputation: 16324

In Scala, methods can have multiple parameter lists:

def f(x: Int)(y: Int, z: String)(w: Boolean) = "foo"
f(1)(2, "bar")(true) //returns "foo"

Multiple parameter lists are useful for several reasons. You can read more about them on this question.

Also in Scala, an empty argument list can be optionally omitted:

def f() = "foo"
f //returns "foo"

The choice of using an empty parameter list is generally governed by convention, as explained in this question.

So, if you have multiple empty argument lists, you can omit any of them:

def f()()() = "foo"
f()()() //returns "foo"
f()() //returns "foo"
f() //returns "foo"
f //returns "foo"

Upvotes: 10

Related Questions