Instance Creation Expressions 

Constructor invocations 

Instance creations are written using NEW(path|typ|"C"):

scala> import treehugger.forest._, definitions._, treehuggerDSL._
import treehugger.forest._
import definitions._
import treehuggerDSL._

scala> object sym {
  val A = RootClass.newClass("A")
}
defined object sym

scala> NEW(sym.A)                   // new A
[1m[34mres0[0m: [1m[32mtreehugger.forest.Tree[0m = New(TypeTree())

scala> NEW("C")                     // new C
[1m[34mres1[0m: [1m[32mtreehugger.forest.Tree[0m = New(TypeTree())

scala> NEW(REF("B") DOT "C")        // new B.C
[1m[34mres2[0m: [1m[32mtreehugger.forest.Tree[0m = New(Select(Ident(B),C))

Optionally, arguments may be passed into the constructor using NEW(path|typ|"C", arg, ...):

scala> val tree = NEW("C", LIT(0), LIT(1))
[1m[34mtree[0m: [1m[32mtreehugger.forest.Tree[0m = Apply(Select(New(TypeTree()),<init>),List(Literal(Constant(0)), Literal(Constant(1))))

scala> treeToString(tree)
[1m[34mres3[0m: [1m[32mString[0m = new C(0, 1)

Anonymous classes 

Anonymous classes are created by passing ANONDEF(parent|"C", ...) into NEW(...):

scala> val tree2 = NEW(ANONDEF() := BLOCK(
  DEF("name") := LIT("Robert")
))
[1m[34mtree2[0m: [1m[32mtreehugger.forest.Tree[0m = New(ClassDef(Modifiers(, , Map()),Modifiers(, , Map()),$anon,List(),List(),Template(List(),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List(DefDef(Modifiers(, , Map()),name,List(),List(),TypeTree(),Literal(Constant(Robert)))))))

scala> val tree3 = NEW(ANONDEF("C") := BLOCK(
  DEF("name") := LIT("Robert")
))
[1m[34mtree3[0m: [1m[32mtreehugger.forest.Tree[0m = New(ClassDef(Modifiers(, , Map()),Modifiers(, , Map()),$anon,List(),List(),Template(List(TypeTree()),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List(DefDef(Modifiers(, , Map()),name,List(),List(),TypeTree(),Literal(Constant(Robert)))))))

scala> val tree4 = NEW(ANONDEF("C") withEarlyDefs(
  VAL("name") := LIT("Robert")
))
[1m[34mtree4[0m: [1m[32mtreehugger.forest.Tree[0m = New(ClassDef(Modifiers(, , Map()),Modifiers(, , Map()),$anon,List(),List(),Template(List(Block(List(),ValDef(Modifiers(, , Map()),Ident(name),Literal(Constant(Robert)))), TypeTree()),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List())))

These examples print as:

scala> treeToString(tree2)
[1m[34mres4[0m: [1m[32mString[0m =
new {
  def name = "Robert"
}

scala> treeToString(tree3)
[1m[34mres5[0m: [1m[32mString[0m =
new C {
  def name = "Robert"
}

scala> treeToString(tree4)
[1m[34mres6[0m: [1m[32mString[0m =
new {
  val name = "Robert"
} with C