相关疑难解决方法(0)

为什么Scala编译器不允许使用默认参数重载方法?

虽然可能存在这样的方法过载可能变得模糊的有效情况,但为什么编译器不允许在编译时和运行时都不模糊的代码?

例:

// 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 …
Run Code Online (Sandbox Code Playgroud)

methods default scala overloading

132
推荐指数
4
解决办法
3万
查看次数

标签 统计

default ×1

methods ×1

overloading ×1

scala ×1