There are two ways to write function applications. The first way is calling APPLY(arg, ...)
on a symbol or a tree. Here, arg
denotes a Tree
:
scala> import treehugger.forest._, definitions._, treehuggerDSL._
import treehugger.forest._
import definitions._
import treehuggerDSL._
scala> val tree = Predef_println APPLY LIT("Hello, world!")
[1m[34mtree[0m: [1m[32mtreehugger.forest.Apply[0m = Apply(Ident(println),List(Literal(Constant(Hello, world!))))
scala> treeToString(tree)
[1m[34mres0[0m: [1m[32mString[0m = println("Hello, world!")
scala> val tree2 = (REF("x") DOT "y") APPLY (LIT(0), LIT(1))
[1m[34mtree2[0m: [1m[32mtreehugger.forest.Apply[0m = Apply(Select(Ident(x),y),List(Literal(Constant(0)), Literal(Constant(1))))
scala> treeToString(tree2)
[1m[34mres1[0m: [1m[32mString[0m = x.y(0, 1)
The second way is to apply arg, ...
on intermediate structure returned by DOT(sym|"y")
:
scala> val tree3 = (REF("x") DOT "y")(LIT(0), LIT(1))
[1m[34mtree3[0m: [1m[32mtreehugger.forest.Apply[0m = Apply(Select(Ident(x),y),List(Literal(Constant(0)), Literal(Constant(1))))
scala> treeToString(tree3)
[1m[34mres2[0m: [1m[32mString[0m = x.y(0, 1)
To pass sequence into a vararg parameter, use SEQARG(arg)
:
scala> val tree4 = THIS APPLY (SEQARG(REF("list")))
[1m[34mtree4[0m: [1m[32mtreehugger.forest.Apply[0m = Apply(This(),List(Typed(Ident(list),Ident(_*))))
scala> treeToString(tree4)
[1m[34mres3[0m: [1m[32mString[0m = this((list: _*))
To pass named arguments into a function, specify the parameter using REF(sym|"x")
as follows:
scala> val tree5 = REF("put") APPLY (REF("x") := LIT(0))
[1m[34mtree5[0m: [1m[32mtreehugger.forest.Apply[0m = Apply(Ident(put),List(Assign(Ident(x),Literal(Constant(0)))))
scala> treeToString(tree5)
[1m[34mres4[0m: [1m[32mString[0m = put(x = 0)
Partially applied functions are written by calling APPLY
with PARTIALLY
:
scala> val tree6 = REF("put") APPLY PARTIALLY
[1m[34mtree6[0m: [1m[32mtreehugger.forest.Apply[0m = Apply(Ident(put),List(Ident(<partially>)))
scala> treeToString(tree6)
[1m[34mres5[0m: [1m[32mString[0m = put _
Note this is different from APPLY
ing WILDCARD
since PARTIALLY
applies to the entire parameter list.