5
scala> def a(i:Int)(j:Int) = i * j
a: (i: Int)(j: Int)Int

scala> def b(i:Int, j:Int) = i * j
b: (i: Int, j: Int)Int

The two definitions are very similar, and they (appear to me) do the same thing.

Apart from defining a function which receives implicit parameters or a code block as parameter, is there any reason to use the first definition style?

2

3 Answers 3

8

This is the list I have compiled over the time:

1) Type resolution across multiple argument lists

class ResourceManager {
  type Resource

  def open: Resource = ???
}

class ResourceManagerTest {
  // Does not compile: def test1(rm: ResourceManager, r: rm.Resource) = ???

  // Compiles: This way the type can be resolved
  def test2(rm: ResourceManager)(r: rm.Resource) = ???
}

2) Type inference where earlier arguments can "lock down" type parameters for later arguments (thanks to Myserious Dan)

  def foo1[A](x: A, f: A => Int) = ???

  def foo2[A](x: A)(f: A => Int) = ???

  def foo1foo2Demo() {
    // This will always demand a type annotation on any anonymous function
    // you pass in:
    foo1(1, (i: Int) => i * i)

    // Does not compile: foo1(1, i => i * i)

    // Type not required
    foo2(2)(i => i * i)
  }

3) Syntax-like language extensions

object MultipleArgumentListsDemo {
  // This style of function definition allows syntax-like language extensions
  @tailrec
  def myWhile(conditional: => Boolean)(f: => Unit) {
    if (conditional) {
      f
      myWhile(conditional)(f)
    }
  }

  def myWhileDemo() {
    var count = 0
    myWhile(count < 5) {
      count += 1
      println(count)
    }
  }

4) Having both implicit and non implicit arguments, as implicit is a modifier for a whole argument list:

  def f[A](x: A)(implicit mf: Manifest[A]) {
  }

5) A parameter's value from one parameter list can be used to compute a default value in another parameter list, but not in the same one.

  def g(x: Int)(y: Int = x * 2) = {
    x + y
  }

6) Multiple repeated argument lists ("varargs")

  def h(as: Int*)(bs: Int*)(cs: Int*) = as.sum * bs.sum * cs.sum

7) Partial application

  def i() {
    val foop = h(1, 2, 3)(4, 5, 6, 7, 9) _
    println(foop(Seq(10, 11)))
 }

As I have not tracked my sources while I was compiling that list over the time: It's possible that some or all examples are copied from elsewhere (other questions on SO), so please drop a note, and I will add the reference as to where it came from.

4
  • 1
    I wouldn't put your first example under type inference. That's a binary choice between not working and working. The more subtle type inference benefit is where earlier arguments can "lock down" type parameters for later arguments. So instead of def foo[A](x: A, f: A => Int), do def foo[A](x: A)(f: A => Int), because the former will always demand a type annotation on any anonymous function you pass in. Aug 8, 2013 at 11:30
  • @MyseriousDan Thanks for your comment: I've changed the first example from inference to resolution. And I've added your example, could you please check, if I have understood it correctly.
    – Beryllium
    Aug 8, 2013 at 12:20
  • The first and the fifth (def g(x: Int)(y: Int = x * 2)) examples look the same to me. Aug 8, 2013 at 12:56
  • Looks good, thanks. @VictorMoroz the difference is that in the first, it's the type referring to the earlier argument, and in the fifth it's the default value. I could see coalescing them to say that you can refer to arguments from earlier argument lists in later ones, I guess. Aug 8, 2013 at 13:06
3

The main reason for "currying" functions in this manner is to enable partial application:

scala> val c = a(5) _
c: Int => Int = <function1>

Here c is a function that takes a single int and returns the result of multiplying that int with 5. It may be that you would set up c in one method, and pass it into another method that expects a function taking one Int parameter. A bit trivial in this case, but very flexible for a range of uses.

2
  • 3
    I don't think that is main reason. More important reasons are operator-like syntax with curly braces and better type inference. See my link above. Aug 8, 2013 at 4:26
  • You can define tbe one parameter given two parameters in one list too: def c(i:Int) = b(5, i) Aug 8, 2013 at 12:20
1

Additional to support currying, it also helps with type inference: Sometimes the compiler can't infer the correct type if everything is in one argument list, but if you split off the part that depends on the binding of the other arguments, it works. A typical example is foldLeft: Try to implement it with one argument list, and then in some cases the compiler needs type annotations.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.