问题描述
在Scala宏中,我想做这样的事情:
In Scala macro, I want to do something like this:
我有一个Tree
(可能很大).现在我想找到这棵树的子树,它有一些具体的形式,例如Apply(_, _)
.现在,我想创建一个新树,该树是原始树的副本,但是找到的子树被其他树替换.
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.
这样可能吗?
推荐答案
我对看到树转换的替代方法非常感兴趣,但是,它们还没有到来(我们实际上正在朝这个方向进行调查).
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).
要同时完成工作,可以扩展Transformer
,重写其transform
方法,然后对感兴趣的树的具体形式进行模式匹配.调用super.transform
递归替换子树.
To get things done in the meanwhile, you can extend Transformer
, override its transform
method and then pattern match against the concrete form of trees you're interested in. Call super.transform
to recursively replace in subtrees.
这篇关于如何用其他树替换子树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!