I want to implement a prototype-based system in Scala. At the root of the type hierarchy is the ROOT node, which has a prototype that refers to itself.
The following code demonstrates what I'm trying to do:
class Node(val prototype: Node) {
private def this() = this(this)
}
object Node {
val ROOT = new Node
}
Unfortunately, this does not compile an error: "this can only be used in a class, object, or template".
The argument "this" for the call to the primary constructor is not accepted. This sounds reasonable, since the object is not yet created. However, since prototype is immutable, I can't set it to null and define it afterwards.
Any suggestions on how to do this properly in Scala?
I'm using Scala-2.8.0RC7.