Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of double method brackets in Scala?

Tags:

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?

like image 371
barbara Avatar asked Mar 07 '15 21:03

barbara


1 Answers

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"
like image 110
Ben Reich Avatar answered Nov 15 '22 21:11

Ben Reich