鉴于此 Scala 代码:
object test {
def byval(a: Int) = println("Int")
def byval(a: Long) = println("Long")
def byname(a: => Int) = println("=> Int")
def byname(a: => Long) = println("=> Long")
def main(args: Array[String]) {
byval(5)
byname(5)
}
}
调用 byval(5) 编译正确,但 byname 编译失败:
ambiguous reference to overloaded definition
为什么?我希望在重载方面观察到按值和按名称参数的相同行为……如何解决?