Étant donné:application de fonction partielle fonctionne prématurément codeblock lorsqu'il est utilisé avec underscore
def save(f: => Any)(run:Boolean) { if (run) { println("running f"); f } else println("not running f") }
Je peux l'appeler avec:
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
Voici le curieux comportement avec application partielle:
save(throw new RuntimeException("boom!"))(_) -> (Boolean) => Unit = <function1> //as expected
save(throw new RuntimeException("boom!")) _ -> exception thrown
Le bloc de code est évalué immédiatement sans être transmis en tant que fonction. Quelle est la différence entre les deux déclarations ci-dessus?
Vous pouvez trouver quelques explications ici: http://stackoverflow.com/questions/2363013/in-scala-why-cant-i-partially-apply-a-function-without-explicitly-specifying-it –
À mon humble avis, c'est un bug. –