Variables

Abstract variable declarations

Abstract variable declarations are written using VAR(...):

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

val tree = (VAR("foo", IntClass): Tree)
// tree: Tree = ValDef(
//   Modifiers(4096L, TypeName(""), List()),
//   Typed(Ident(TermName("foo")), TypeTree()),
//   EmptyTree
// )

treeToString(tree)
// res0: String = "var foo: Int"

or in general:

VAR(sym|"x", typ|"C").tree

Variable definitions

Value definitions are written as:

val tree2 = VAR("foo", IntClass) := LIT(0)
// tree2: ValDef = ValDef(
//   Modifiers(4096L, TypeName(""), List()),
//   Typed(Ident(TermName("foo")), TypeTree()),
//   Literal(Constant(0))
// )

treeToString(tree2)
// res1: String = "var foo: Int = 0"

as a special form of initializing the variable with the default value of the type, WILDCARD can be used as follows:

val tree3 = VAR("foo", IntClass) := WILDCARD
// tree3: ValDef = ValDef(
//   Modifiers(4096L, TypeName(""), List()),
//   Typed(Ident(TermName("foo")), TypeTree()),
//   Ident(TermName("_"))
// )

treeToString(tree3)
// res2: String = "var foo: Int = _"