Created
January 23, 2012 08:18
-
-
Save Rogach/1661713 to your computer and use it in GitHub Desktop.
Non-homogeneous tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package socco | |
abstract class SoccoTree(val children:List[SoccoTree]) extends Seq[SoccoTree] with SoccoNode[SoccoTree] { | |
def apply(n:Int) = children(n) | |
def iterator = children.iterator | |
def length = children.length | |
// def add(t:SoccoTree)(implicit builder:SoccoTreeBuilder[B]) = builder.build(this.asInstanceOf[B], children :+ t) | |
} | |
trait SoccoNode[B <: SoccoTree] { | |
def soccoCompanion:SoccoTreeCompanion[B] | |
} | |
trait SoccoTreeCompanion[A <: SoccoTree] { | |
implicit val builder:SoccoTreeBuilder[A] | |
} | |
trait SoccoTreeBuilder[A <: SoccoTree] { | |
def build(node:A, children:List[SoccoTree]):A | |
} | |
object Directory extends SoccoTreeCompanion[Directory] { | |
def apply(name:String) = new Directory(name,List()) | |
implicit val builder = new SoccoTreeBuilder[Directory] { | |
def build(node:Directory, children:List[SoccoTree]) = new Directory(node.name, children) | |
} | |
} | |
class Directory(val name:String, | |
val _children:List[SoccoTree]) extends SoccoTree(_children) with SoccoNode[Directory] { | |
def soccoCompanion = Directory | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment