문제

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?
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top