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

后端 未结 2 863
执念已碎
执念已碎 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:36

    The difference is whether _ stands for the whole parameter, or is part of an expression. Depending on which, it falls into one of the two following categories:

    Partially Applied Function

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

    translates into

    Seq(fromDir, toDir) find (!_.isDirectory) foreach ((x$1) => println(x$1))
    

    Anonymous Function Parameter

    Seq(fromDir, toDir) find (!_.isDirectory) foreach (throw new Exception(_.toString))
    

    translates into

    Seq(fromDir, toDir) find (!_.isDirectory) foreach (throw new Exception((x$1) => x$1.toString))
    

提交回复
热议问题