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?
You can't do that.
Look at this piece of code:
There is no way that both of these lines could 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:
I'm afraid there is no way to do that. The
super[A].work
way works only ifA
andB
have the same return types.Consider this:
You can't do that in Scala.
The way to work this around is to use the traits as collaborators
Scala simply prevents you from mixing
A
andB
together if they declare a method with the same name and incompatible signature.