Playing about with a DSL in Scala, so lets say I have something like this:
house {
floor {
bedroom(\"kids)
bedroom(\"master\")
}
floor {
ki
Do you really need each block to have a reference to the enclosing block? Or was it just so that you could add the nested block to the parent block? In this case you could simply pass nested blocks to the enclosing block, so to speak:
house (
floor (
bedroom("kids"),
bedroom("master")
),
floor (
kitchen
)
)
Using the following definitions:
trait HouseElement
case class house( elements: HouseElement* )
trait FloorElement
case class floor( elements: FloorElement * ) extends HouseElement
case class bedroom( name: String ) extends FloorElement
case object kitchen extends FloorElement
Otherwise, another solution is to rely heavily on anonymous classes (which unfortunately requires to use the new
keyword everywhere):
new house {
new floor {
new bedroom("kids")
new bedroom("master")
}
new floor {
new kitchen()
}
}
Using the following definitions:
import collection.mutable.Buffer
class house {
val elements = Buffer[Element]()
trait Element {
elements += this
}
class floor extends Element {
val elements = Buffer[Element]()
trait Element {
elements += this
}
class bedroom(name: String) extends Element
class kitchen extends Element
}
}