How to replace a subtree with nothing in a visit?
In Pattern With Action, we see that
case red(l, r) => green(l, r)
replaces all red nodes with green nodes. I want to do something like
case red(l, r) => []
to remove all red nodes. Is that possible?
Interesting question. So if the abstract grammar is this:
Any
visitis not allowed to change the type of the node it replaces. So a red node can become green, or black or white, but not "nothing". You could add a definition of nothing:And then rewrite all red nodes to
nothing:So that's not really nothing :-)
However, a more common case is to remove elements from a list. In abstract syntax trees for programming languages this often happens:
For example if the list of statements of an if-then-else statement, etc.
Now you can match on the list to remove elements:
And now the red node is really gone.