Classes and objects are both defined in terms of templates, whose body is represented using BLOCK(...)
in treehugger DSL.
In general, treehugger DSL uses BLOCK(...)
wherever curly braces ({}
) appear in Scala. BLOCK(...)
accepts vararg of trees, such as class member definitions and expressions:
scala> import treehugger.forest._, definitions._, treehuggerDSL._
import treehugger.forest._
import definitions._
import treehuggerDSL._
scala> object sym {
val IntQueue: ClassSymbol = RootClass.newClass("IntQueue")
}
defined object sym
scala> val tree = CLASSDEF(sym.IntQueue) withFlags(Flags.ABSTRACT) := BLOCK(
DEF("get", IntClass),
PROC("put") withParams(PARAM("x", IntClass))
)
[1m[34mtree[0m: [1m[32mtreehugger.forest.ClassDef[0m = ClassDef(Modifiers(abstract, , Map()),Modifiers(, , Map()),IntQueue,List(),List(),Template(List(),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List(DefDef(Modifiers(, , Map()),get,List(),List(),TypeTree(),EmptyTree), ProcDef(Modifiers(, , Map()),put,List(),List(List(ValDef(Modifiers(<param>, , Map()),Typed(Ident(x),TypeTree()),EmptyTree))),EmptyTree))))
This example prints as:
scala> treeToString(tree)
[1m[34mres0[0m: [1m[32mString[0m =
abstract class IntQueue {
def get: Int
def put(x: Int)
}