In Scala macro, I want to do something like this:
I have a Tree (possibly large). Now I want to find a subtree of this tree that has some concrete form, e.g. Apply(_, _). And now I want to create a new tree that is a copy of the original tree, but the found subtree is replaced by some other tree.
With something like this, I could for example replace invocation of some method with invocation of some other method.
Is something like this possible?
I’m very interested in seeing alternative approaches to tree transformation, however, they have yet to arrive (and we actually have an ongoing investigation in this direction).
To get things done in the meanwhile, you can extend
Transformer, override itstransformmethod and then pattern match against the concrete form of trees you’re interested in. Callsuper.transformto recursively replace in subtrees.