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

旧时模样 提交于 2019-11-30 22:07:06

问题


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?


回答1:


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



回答2:


Scala simply prevents you from mixing A and B together if they declare a method with the same name and incompatible signature.




回答3:


You can't do that.

Look at this piece of code:

val c = new C
val a: A = c
val b: B = c

There is no way that both of these lines could work:

val s: String = a.work
val i: Int = b.work

If we allowed such code to compile, one of these assignments would have to throw a ClassCastException or fail in another way. So, it simply isn't possible to resolve such conflict.

I guess you have to workaround this with some form of delegation, maybe something like this:

class C extends A {
  def toB = new B {
    //implement B methods by somehow delegating them to C instance
  }
}



回答4:


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
}


来源:https://stackoverflow.com/questions/19153088/in-scala-multiple-inheritance-how-to-resolve-conflicting-methods-with-same-sign

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!