Suppose I have such trait:
trait FooStackable {
def foo: String
def bar: Double
abstract override def logic(x: Double): Unit = {
if (x < 0) println(foo)
else super.logic(bar + x)
}
}
logic
logic
The code cannot be compiled as written. In particular, there is no 'super' in this trait as it is not derived from anything. I made some changes to work around it:
trait Stackable {
def logic(x: Double): Unit
}
trait FooStackable extends Stackable {
def foo: String
def bar: Double
abstract override def logic(x: Double): Unit = {
println("In FooStackable.logic()")
if (x < 0) println(foo)
else super.logic(bar + x)
}
}
trait TestStackable extends Stackable {
def logic(x: Double): Unit = {
println("In TestStackable.logic()")
}
}
object FooStackableTest extends TestStackable with FooStackable {
def foo: String = "foo"
def bar: Double = 1.234
override def logic(x: Double): Unit = {
println("In FooStackableTest.logic()")
super.logic(x)
}
}
With these changes FooStackable now sits in the middle between TestStackable (which implements logic() in a base class) and FooStackableTest (implements other undefined members of FooStackable and overrides logic()):
scala> FooStackableTest.logic(4.0)
In FooStackableTest.logic()
In FooStackable.logic()
In TestStackable.logic()
scala> FooStackableTest.logic(-4.0)
In FooStackableTest.logic()
In FooStackable.logic()
foo
scala>