Case this works:
Seq(fromDir, toDir) find (!_.isDirectory) foreach (println(_))
Whereas this doesn’t:
Seq(fromDir, toDir) find (!_.isDirectory) foreach (throw new Exception(_.toString))
Compilation ends with this error:
error: missing parameter type for expanded function ((x$4) => x$4.toString)
Now if I write it this way it compiles again:
Seq(fromDir, toDir) find (!_.isDirectory) foreach (s => throw new Exception(s.toString))
I am sure there is a reasonable explanation 😉
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 toExceptionin the error case is therefore, after expansion, the full anonymous function(x$1) => x$1.toStringof typeA <: Any => String, whileExceptionexpects aString.In the
printlncase,_by itself isn’t of syntactic categoryExpr, but(println (_))is, so you get the expected(x$0) => println(x$0).