9

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?

barbara
  • 3,111
  • 6
  • 30
  • 58

1 Answers1

11

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"
Community
  • 1
  • 1
Ben Reich
  • 16,222
  • 2
  • 38
  • 59
  • Yes, multiple parameter lists is a form of currying. Read more about currying in Scala: http://www.codecommit.com/blog/scala/function-currying-in-scala – Ben Reich Mar 07 '15 at 22:15