【发布时间】:2014-11-19 12:40:06
【问题描述】:
我注意到 Scala 编译器的一个奇怪行为。代码:
Seq("?").toSet foreach (println(_))
产生以下错误:
error: missing parameter type for expanded function ((x$1) => println(x$1))
Seq("?").toSet foreach (println(_))
^
同样的情况:
Seq("?").toSet foreach (x => println(x))
我找到了两种解决方法。 Ether 明确指定类型:
Seq("?").toSet[String] foreach (println(_))
或者保存到变量中:
val s = Seq("?").toSet
s foreach (println(_))
这是合理的行为还是编译器错误?这对我来说没有多大意义。这怎么解释?
【问题讨论】:
-
有趣的是,荒谬的替代方案
Seq("?").toSet forall {x => println(x); true}编译并运行,而Seq("?").toSet foreach {x => println(x); true}失败并出现missing parameter type编译器错误。println似乎不是问题,因为您可以将函数更改为x => true并且仍然有forall运行和foreach产生错误。
标签: scala compiler-errors