Classes and Objects

Templates

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:

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

object sym {
  val IntQueue: ClassSymbol = RootClass.newClass("IntQueue")  
}

val tree = CLASSDEF(sym.IntQueue) withFlags(Flags.ABSTRACT) := BLOCK(
  DEF("get", IntClass),
  PROC("put") withParams(PARAM("x", IntClass))
)

This example prints as:

treeToString(tree)
// res0: String = """abstract class IntQueue {
//   def get: Int
//   def put(x: Int)
// }"""