I have a normal tree defined in Scala.
sealed abstract class Tree
object Tree {
case class Node (...) extends Tree
case class Leaf (...) extends Tree
}
Now I want to add a member variable to all nodes and leaves in the tree. Is this possible with the extend keyword, or do I need to change the tree classes by adding [T]?
Update:
It seems that my question was misinterpreted. An example should clear it:
I need this tree structure (actually something more complex) has two doubles in the same context. In another context, I need it to have one line. And yet in another context, I need a clean tree without any (additional) members. And I would like the first two options to be the third option. Pseudocode:
DTree extends Tree with Touple2[Double, Double]
object DTree {
case class Node (...) extends Tree.Node with Touple2[Double, Double]
case class Leaf (...) extends Tree.Leaf with Touple2[Double, Double]
}
STree extends Tree with String
object DTree {
case class Node (...) extends Tree.Node with String
case class Leaf (...) extends Tree.Leaf with String
}
...
def f (t : Tree) { ... }
I want f to be able to process all trees.