【发布时间】:2015-11-24 12:18:59
【问题描述】:
是什么导致以下代码在 Scala 2.11 中产生编译错误?
class Foo {
def fn(a: Seq[Int], b: Int => Int) {}
// Comment this line out
def fn(a: Map[String, String], c: String => Double) {}
}
object Bar {
def main(args: Array[String]) {
val f = new Foo()
f.fn(Seq(1, 2), _ * 2)
}
}
错误:
Error:(9, 21) missing parameter type for expanded function ((x$1) => x$1.$times(2))
f.fn(Seq(1, 2), _ * 2)
^
但如果 fn 的 Map[String, String], String => Double 版本被注释掉,代码仍然可以编译。我可以通过给编译器一两个提示来轻松解决这个问题,但我从不喜欢在 Scala 中显式指定类型:-)
【问题讨论】:
-
很遗憾听到您从不喜欢指定类型。这极大地影响了可读性
-
@Odomontois 这值得商榷 - Haskell 是完全推断出来的,而且 IMO 的可读性也不差。
-
@dcastro 虽然在 Haskell 中指定类型也是不错的行为。
-
@dcastro 根据我的经验并没有完全推断出来,至少在具有单态限制的 GHC 中
-
重载和类型推断是天敌。
标签: scala overloading