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

后端 未结 7 1796
失恋的感觉
失恋的感觉 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:57

    One of the possible scenario is

    
      def foo(a: Int)(b: Int = 10)(c: String = "10") = a + b + c
      def foo(a: Int)(b: String = "10")(c: Int = 10) = a + b + c
    

    The compiler will be confused about which one to call. In prevention of other possible dangers, the compiler would allow at most one overloaded method has default arguments.

    Just my guess:-)

提交回复
热议问题