I want to build a list from items provided by individual traits. I can do this using stackable traits for this purpose:
trait StackableBuilder {
def build: Seq[String] = Seq.empty
def buildAll: Seq[String] = build
}
trait BuildA extends StackableBuilder {
abstract override def build = super.build :+ "A"
}
trait BuildB extends StackableBuilder{
abstract override def build = super.build :+ "B"
}
object AB extends StackableBuilder with BuildA with BuildB
println(AB.buildAll)
Still, I would like to improve this a bit, so that each partial builder does not have to repeat the super.build
call, something along the lines:
trait StackableBuilder {
def build: String
def buildAll: Seq[String] = ???
}
trait BuildA extends StackableBuilder {
def build = "A"
}
trait BuildB extends StackableBuilder {
def build = "B"
}
object AB extends StackableBuilder with BuildA with BuildB
println(AB.buildAll)
Now this does not compile, the error is "object AB inherits conflicting members", and even if it did, still the part ???
needs to be filled.
Is it possible somehow to define stackable traits so that the stacking using super calls does not have to be done by the individual traits, perhaps introducing some helper trait and method, perhaps a bit like How to call super method when overriding a method through a trait?