Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 537
Marcin Avatar asked Oct 03 '13 07:10

Marcin


1 Answers

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
like image 94
serejja Avatar answered Sep 22 '22 10:09

serejja