问题描述
这种情况下:
Seq(fromDir, toDir) find (!_.isDirectory) foreach (println(_))
然而这不是:
Seq(fromDir, toDir) find (!_.isDirectory) foreach (throw new Exception(_.toString))
编译以这个错误结束:
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 ;)
推荐答案
这已经在 一个相关问题.下划线向外延伸到最接近的结束 Expr
:顶级表达式或括号中的表达式.
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)
是括号中的表达式.因此,您在错误情况下传递给 Exception
的参数在扩展后是完整的匿名函数 (x$1) =>
,而 A 类型的 x$1.toString
StringException
需要一个 String
.
(_.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
.
在 println
的情况下,_
本身不属于语法类别 Expr
,而是 (println (_))
是,所以你得到预期的 (x$0) =>println(x$0)
.
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)
.
这篇关于为什么我得到一个“缺少扩展函数的参数"?在一种情况下而不是另一种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!