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

Marcin picture Marcin · Oct 3, 2013 · Viewed 7.5k times · Source

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
}

Class C won't compile in scala 2.10, because of "overriding method work in trait A of type => String; method work has incompatible type".

How to choose one specific method?

Answer

serejja picture serejja · Oct 3, 2013

I'm afraid there is no way to do that. The super[A].work way works only if A and B have the same return types.

Consider this:

class D extends B

....

val test: List[B] = List(new C(), new D())
test.map(b => b.work) //oops - C returns a String, D returns an Int