Partial function application prematurely runs codeblock when used with underscore

后端 未结 2 1301
终归单人心
终归单人心 2021-01-12 01:05

Given:

def save(f: => Any)(run:Boolean) { if (run) { println(\"running f\"); f } else println(\"not running f\") } 

I can call it with:<

2条回答
  •  执笔经年
    2021-01-12 01:35

    The behaviour of call-by-name parameters under eta expansion is currently under review, see this bug. Your code works as you expect (that is, the line save(throw new RuntimeException("boom!")) _ returns a function without throwing the exception) with recent nightly builds of 2.10. Let's see if it stays in until release!

    See also this question for a related question on the general case of eta expansion not involving call-by-name.

提交回复
热议问题