【问题标题】:Missing parameter type for expanded function when not assigned to a variable [duplicate]未分配给变量时缺少扩展函数的参数类型[重复]
【发布时间】: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


【解决方案1】:

它扩展为:

Seq("?").toSet foreach (println(x=>x))

【讨论】:

  • 虽然这是一个有效的替代方案,但它不能回答问题。
  • 纯代码答案会自动标记为低质量,因此不鼓励在 stackoverflow 上使用。以后请用细节修饰你的答案,并解释为什么它是问题的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多