虽然可能存在这样的方法重载可能变得不明确的有效情况,但是为什么编译器不允许在编译时和运行时都不明确的代码呢?
例如:
// This fails:
def foo(a: String)(b: Int = 42) = a + b
def foo(a: Int) (b: Int = 42) = a + b
// This fails, too. Even if there is no position in the argument list,
// where the types are the same.
def foo(a: Int) (b: Int = 42) = a + b
def foo(a: String)(b: String = "Foo") = a + b
// This is OK:
def foo(a: String)(b: Int) = a + b
def foo(a: Int) (b: Int = 42) = a + b
// Even this is OK.
def foo(a: Int)(b: Int) = a + b
def foo(a: Int)(b: String = "Foo") = a + b
val bar = foo(42)_ // This complains obviously ...
为什么这些限制一点也不能放松呢?
特别是当将大量重载的 Java 代码转换为 Scala 默认参数是非常重要的,并且在用一个 Scala 方法替换了大量 Java 方法之后发现 spec/编译器强加了任意的限制是不好的。