标签云

微信群

扫码加入我们

WeChat QR Code

According to ScalaByExample:A curried function definition def f (args1) ... (argsn) = E where n >1 expands todef f (args1) ... (argsn−1) = { def g (argsn) = E ; g }Or, shorter, using an anonymous function:def f (args1) ... (argsn−1) = ( argsn ) => EUncurried version:def f(x: Int): Int => Int = {y: Int => x * y} //> f: (x: Int)Int => Intdef f1 = f(10)//> f1: => Int => Intf1(5) Curried version:def g(x: Int)(y: Int) = {x * y} //> g: (x: Int)(y: Int)Intdef g1 = g(10) _//> g1: => Int => Intg1(5)The question is, Why curried required the underscore in line #5 in the second code snippet.


My guess is because otherwise the compiler wouldn't really know if you just forgot a parameter to your function or if you really meant it as a partial function. I'm sure there's a much more in depth answer though.

2019年04月18日09分06秒

I read an answer like yours, but I still not fully convinced, the return type in this case will not be Int, instead it will be Int => Int

2019年04月18日09分06秒

println(g(10)) won't compile cause println has no overloading method that accepts Int => Int as parameter.

2019年04月19日09分06秒

I meant that if g(10) _ was not required, println(g(10)) will compile and you won't notice a mistake. Sorry, it's not obvious from my answer, I will make a necessary corrections

2019年04月18日09分06秒