Traits are defined using TRAITDEF(...)
:
scala> import treehugger.forest._, definitions._, treehuggerDSL._
import treehugger.forest._
import definitions._
import treehuggerDSL._
scala> val tree = TRAITDEF("Philosophical") := BLOCK(
PROC("philosophize") := BLOCK(
LIT(0)
)
)
[1m[34mtree[0m: [1m[32mtreehugger.forest.ClassDef[0m = ClassDef(Modifiers(<defaultparam/trait>, , Map()),Modifiers(, , Map()),Philosophical,List(),List(),Template(List(),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List(ProcDef(Modifiers(, , Map()),philosophize,List(),List(),Block(List(),Literal(Constant(0)))))))
scala> val tree2 = (CLASSDEF("Animal"): Tree)
[1m[34mtree2[0m: [1m[32mtreehugger.forest.Tree[0m = ClassDef(Modifiers(, , Map()),Modifiers(, , Map()),Animal,List(),List(),Template(List(),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List()))
scala> val tree3 = (TRAITDEF("HasLegs"): Tree)
[1m[34mtree3[0m: [1m[32mtreehugger.forest.Tree[0m = ClassDef(Modifiers(<defaultparam/trait>, , Map()),Modifiers(, , Map()),HasLegs,List(),List(),Template(List(),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List()))
scala> val tree4 = (CLASSDEF("Frog")
withParents("Animal", "HasLegs", "Philosophical") := BLOCK(
DEF(Any_toString) withFlags(Flags.OVERRIDE) := LIT("green")
))
[1m[34mtree4[0m: [1m[32mtreehugger.forest.ClassDef[0m = ClassDef(Modifiers(, , Map()),Modifiers(, , Map()),Frog,List(),List(),Template(List(TypeTree(), TypeTree(), TypeTree()),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List(DefDef(Modifiers(override <method>, , Map()),toString,List(),List(),TypeTree(),Literal(Constant(green))))))
These print as:
scala> treeToString(tree)
[1m[34mres0[0m: [1m[32mString[0m =
trait Philosophical {
def philosophize {
0
}
}
scala> treeToString(tree2)
[1m[34mres1[0m: [1m[32mString[0m = class Animal
scala> treeToString(tree3)
[1m[34mres2[0m: [1m[32mString[0m = trait HasLegs
scala> treeToString(tree4)
[1m[34mres3[0m: [1m[32mString[0m =
class Frog extends Animal with HasLegs with Philosophical {
override def toString = "green"
}