Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between similar curried types in Scala

Tags:

scala

What is the difference between the types of the following two functions?

def add1: Int => Int => Int = a => b => a + b
def add2(a: Int)(b: Int) = a + b

Based on their declarations, they seem to have the same type. Both are called in the same way:

scala> add1(1)(2)
res2: Int = 3

scala> add2(1)(2)
res3: Int = 3

However, there is an apparent difference in their types:

scala> :t add1
Int => Int => Int

scala> :t add2
(a: Int)(b: Int)Int

Additionally, partial application of add1 is a bit cleaner than of add2.

scala> add1(1)
res4: Int => Int = <function1>

scala> add2(1)(_)
res5: Int => Int = <function1>
like image 758
earldouglas Avatar asked Oct 09 '11 17:10

earldouglas


1 Answers

add1 is a method with no parameters that returns a Function1[Int, Function1[Int, Int]]. add2 is a method that takes two parameter lists and returns an Int.

Further reading:

Difference between method and function in Scala

like image 120
retronym Avatar answered Sep 18 '22 07:09

retronym