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 it possible with extend keyword or do I have to modify the tree classes by adding [T]?
Update:
It seems that my question was misunderstood.
Example should clear it up:
I need this Tree structure (actually something more complicated) have two Doubles in one context. In another context I need it to have one string. And yet in another context I need pure tree without any (additional) members. And I would like first two variants to be the third variant. 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 handle all the trees.