I'm trying to write a method in a trait that's recursive on types, but I'm running into the problem that I can't write the base case in such a way that the compiler is convinced I know what I'm doing.
Here's a gist:
trait InheritableBehavior {
def superClassWithInheritableBehavior: Option[Class[_]] = {
// reflection and magic (from some library's code)
}
def doSomething() {
if (this.superClassWithInheritableBehavior.isDefined) {
super.doSomething()
} else {
// base case here, which relies on local fields
}
}
}
The compiler suggests I mark doSomething()
with abstract override
, but there is no concrete implementation. Everything is in that one method.
I suppose I could split the trait into two--BaseInheritableTrait
, which doesn't call super
, and DerivedInheritableTrait
which includes the super
call, but is there any better way to deal with this?
What I'd love to be able to do is super.asInstanceOf[InheritableTrait].doSomething()
, but that doesn't seem to be possible.