In scala multiple inheritance, how to resolve conflicting methods with same signature but different return type?

前端 未结 4 1867
时光说笑
时光说笑 2020-12-17 10:50

Consider the code below:

trait A {
  def work = { \"x\" }
}

trait B {
  def work = { 1 }
}

class C extends A with B {
  override def work = super[A].work
}         


        
4条回答
  •  萌比男神i
    2020-12-17 11:14

    You can't do that in Scala.

    The way to work this around is to use the traits as collaborators

    trait A {
      def work = { "x" }
    }
    
    trait B {
      def work = { 1 }
    }
    
    class C {
      val a = new A { }
      val b = new B { }
    
      a.work
      b.work
    }
    

提交回复
热议问题