I'm trying to make "trait bundles" that I can mix together, which mix traits contained within them. The following approach doesn't seem to work. Can you suggest an approach that can work?
trait GraphTypes {
trait Node {
def size: Double
}
}
trait HasBigAndSmallNodes extends GraphTypes {
trait BigNode extends super.Node {
override def size = 5.00
}
trait SmallNode extends super.Node {
override def size = 0.05
}
}
trait NodesHaveHappiness extends GraphTypes {
trait Node extends super.Node {
def happiness = size * 2.0 // Ideally, .happiness would mix in
} // to BigNode and SmallNode.
}
object ACertainGraph extends GraphTypes
with NodesHaveHappiness with HasBigAndSmallNodes
{
val big = new BigNode { }
val small = new SmallNode { }
println(big.happiness) // error: value happiness is not a member of Graph.BigNode
}
This doesn't work, I think because there's nothing that says that BigNode
and SmallNode
extend NodesHaveHappiness.Node
(but see this question). I don't want to write that in explicitly, though. I want to be able to put node types into separate "trait bundles" from "trait bundles" that define node attributes, and mix them together (somehow) so that all the node types get all the node attributes.
How do you keep this manageable in Scala?