Приложение с частичной функцией преждевременно запускает кодовый блок, когда используется с подчеркиванием

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 expected
save(throw new RuntimeException("boom!")) _ -> exception thrown

The codeblock is evaluated immediately without being passed in as a function. What is the difference between the above 2 statements?

9
задан Andrey Tyukin 22 February 2019 в 20:35
поделиться