scalafunctional-programmingcurryingpartially-applied-type

scala method vs function partial application


Hereby, I would like to understand the difference between:

val aCurriedfunc: Int => String => String = x => y => x + " " + y 
aCurriedfunc (2) 

and

def aCurriedMethod (x:Int) (y: String) = x + " " + y
aCurriedMethod (2) _ 

Indeed why is it that the first case required no "_" but the second case requires it. Yes one is a function with a type and the other a method which has no real type in Sscala from what I understood. However this distinction just lead me to a second question.

If yes


Solution

  • The _ in curriedMethod (2) _ asks the compiler to perform eta-expansion. The result of this is a function, afterwards there is no way (or need) to distinguish between a partially applied function and the result of eta expansion.

    The separate parameter lists in a method like curriedMethod are actually implemented as a single method with all the parameters combined. Eta-expansion would be needed to make the method into a function anyway, so the partial expansion is implemented by letting the closure created by eta-expansion close over the partially-applied parameters.