eta-expansion

Partial function application prematurely runs codeblock when used with underscore

江枫思渺然 提交于 2019-12-01 00:11:12
问题 Given: def save(f: => Any)(run:Boolean) { if (run) { println("running f"); f } else println("not running f") } I can call it with: save("test")(true) -> running f save("test")(false) -> not running f save(throw new RuntimeException("boom!"))(false) -> not running f save(throw new RuntimeException("boom!"))(true) -> running f and then exception thrown Here's the curious behaviour with partial application: save(throw new RuntimeException("boom!"))(_) -> (Boolean) => Unit = <function1> //as

What is the eta expansion in Scala?

走远了吗. 提交于 2019-11-26 13:45:23
I am new to Scala. I just heard the term "eta expansion" and roughly know that it means to expand a method to a function object. But I find few resources in SO that systematically introduce it. I am curious about how eta expansion works in Scala. What are the scenarios that eta expansion are needed? And how eta expansion is implemented in Scala? I roughly know that in cases like this: def someMethod(x: Int): Int = x * x someMethod _ will roughly be translated to a new function object like this: new Function1[Int, Int] { def apply(x: Int): Int = x * x } Is it all that Scala does? The definition

What is the eta expansion in Scala?

∥☆過路亽.° 提交于 2019-11-26 02:58:07
问题 I am new to Scala. I just heard the term \"eta expansion\" and roughly know that it means to expand a method to a function object. But I find few resources in SO that systematically introduce it. I am curious about how eta expansion works in Scala. What are the scenarios that eta expansion are needed? And how eta expansion is implemented in Scala? I roughly know that in cases like this: def someMethod(x: Int): Int = x * x someMethod _ will roughly be translated to a new function object like