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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With