1

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.

  • Does any of this has something to do with eta expansion?

If yes

  • How to distinguish between partially applied function and eta expansion?
Beryllium
  • 12,808
  • 10
  • 56
  • 86
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
  • 1
    Take a look here first: [Scala currying vs partially applied functions](http://stackoverflow.com/questions/14309501/scala-currying-vs-partially-applied-functions) [and here](http://stackoverflow.com/questions/8650549/using-partial-functions-in-scala-how-does-it-work/8650639#8650639) – goral Apr 23 '14 at 21:05
  • There are no curried methods. The nature of currying and of Scala and its implementation on the JVM mean that only Function may be curried. Partial application can "lift" a method to a function at which time it could become curried (by use of the `curried` method on `FunctionN` for `N` > 1). – Randall Schulz Apr 23 '14 at 22:30

1 Answers1

3

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.

wingedsubmariner
  • 13,350
  • 1
  • 27
  • 52