Say I have an expression whose tree I want to capture (without evaluating it!) and then pass it into a another macro sometime later:
// capture a tree from some arbitrary expression
val capturedTree = captureMacro( some(expression(tree())) )
// plug the tree into here
val result = processMacro(capturedTree, otherStuff)
Is there any way to do this? I've tried making capturedTree
a refined type hoping that this would preserve the initial tree but that did not happen:
trait WithCapturedTree { def tree:Any }
class ReturnTreeMacro(val c:MacroContext) {
import c.universe._
def run(expression: Tree) =
q"new WithCapturedTree {val tree = ${expression}}"
}
def returnTree(expression:String):WithCapturedTree = macro ReturnTreeMacro.run
reify { returnTree("foo"+"bar") }
// returns Expr[WithCapturedTree{val tree: java.lang.String}](returnTree("foobar"))
// I.e. the result of "foo"+"bar" has already been evaluated!
Is there a way to get this approach to work? Is there a better appraoch for this problem?