Why do I get a “missing parameter for expanded function” in one case and not the other?

后端 未结 2 866
执念已碎
执念已碎 2020-12-14 02:19

Case this works:

Seq(fromDir, toDir) find (!_.isDirectory) foreach (println(_))

Whereas this doesn\'t:

Seq(fromDir, toDir)          


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 02:46

    This has already been addressed in a related question. Underscores extend outwards to the closest closing Expr : top-level expressions or expressions in parentheses.

    (_.toString) is an expression in parentheses. The argument you are passing to Exception in the error case is therefore, after expansion, the full anonymous function (x$1) => x$1.toString of type A <: Any => String, while Exception expects a String.

    In the println case, _ by itself isn't of syntactic category Expr, but (println (_)) is, so you get the expected (x$0) => println(x$0).

提交回复
热议问题