0

In following definitions, I can use {} instead of () for curried functions but not for regular functions. Why?

//curried
scala> def add(i:Int)(j:Int) = {i+j}
add: (i: Int)(j: Int)Int

scala> add(1)(2)
res19: Int = 3

//{} works
scala> add(1){2}
res20: Int = 3

//{} works again
scala> add{1}{2}
res21: Int = 3

//non curried
scala> def add(i:Int,j:Int) = {i+j}
add: (i: Int, j: Int)Int

//use of {} creates error
scala> add{1,2}
<console>:1: error: ';' expected but ',' found.
add{1,2}
     ^
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184

1 Answers1

4

{} is code block, the last value in code block is the return value of this.

  • for add(1){2} means the second code block return value 2 as the second parameter

  • for add{1}{2} means: first code block return 1 as parameter, second code block return 2 as second parameter

so for add{1,2} doesn't have any meaning, this is not a correct syntax.

chengpohi
  • 14,064
  • 1
  • 24
  • 42