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

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

    I can't answer your question, but here is a workaround:

    implicit def left2Either[A,B](a:A):Either[A,B] = Left(a)
    implicit def right2Either[A,B](b:B):Either[A,B] = Right(b)
    
    def foo(a: Either[Int, String], b: Int = 42) = a match {
      case Left(i) => i + b
      case Right(s) => s + b
    }
    

    If you have two very long arg lists which differ in only one arg, it might be worth the trouble...

提交回复
热议问题