带有两个重载方法编译的示例代码(Scala 2.11.8),如预期的那样:
def foo(p: Int, t: Double): Unit = {}
def foo(r: Int => Double): Unit = {}
foo(0, 0) // 1st method
foo(x => x.toDouble) // 2nd method
添加了通用参数的相同代码:
def fooGeneric[T](p: T, t: Double): Unit = {}
def fooGeneric[T](r: T => Double): Unit = {}
fooGeneric[Int](1, 1) // 1st method
fooGeneric[Int]((x: Int) => x.toDouble) //2nd method
fooGeneric[Int](x => x.toDouble) // does not compile
产生以下编译错误:
Error:(112, 19) missing parameter type
fooGeneric[Int](x => x.toDouble)
方法解析似乎没有歧义,但编译器无法找到匹配项。为什么呢?