Why does the Scala compiler disallow overloaded methods with default arguments?

后端 未结 7 1808
失恋的感觉
失恋的感觉 2020-11-29 19:26

While there might be valid cases where such method overloadings could become ambiguous, why does the compiler disallow code which is neither ambiguous at compile time nor at

7条回答
  •  再見小時候
    2020-11-29 19:53

    What worked for me is to redefine (Java-style) the overloading methods.

    def foo(a: Int, b: Int) = a + b
    def foo(a: Int, b: String) = a + b
    def foo(a: Int) = a + "42"
    def foo(a: String) = a + "42"
    

    This ensures the compiler what resolution you want according to the present parameters.

提交回复
热议问题