Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.
Already on GitHub?
to your account
iex> expr = quote do
...> if !a, do: oneway, else: anotherway
iex> IO.puts Macro.to_binary(Macro.expand(expr, __ENV__))
iex> Macro.expand(expr, __ENV__)
The text was updated successfully, but these errors were encountered:
What is supposed to be the bug here? Notice that Macro.expand does not expand recursively.
Sorry, something went wrong.
I guess what I was looking for is recursive macro expansion... anyway, why does it expand ! to !! ?
if actually expands to case(!...), that's why you get two !!. Btw, if you can probably loop through the expressions if you want to expand it recursively. I have updated the docs to be clear about it.
No way we can support optional recursive expansion? Doesn't make much sense?
Maybe with another function that does it explicitly, like expand_all. But I
haven't needed it myself, so I don't have plans to work on it.
I expected Macro.expand to expand recursively, so that I had to do googling a bit of time to find this issue.
Seems a little bit confusing because there's Macro.expand_once.
In Macro.expand_once doc,
Notice that expand_once/2 performs the expansion just once and it is not recursive.
Check expand/2 for expansion until the node can no longer be expanded.
It says Macro.expand_once is not recursive and recommend to check Macro.expand for such a case. It sounds like Macro.expand would expand recursively...
Macro.expand recurses but it does not traverse.
For anyone that needs to recursively expand, I use this all over the place in my macro libraries (it is surprising just how often I need it actually...):
expanded_ast = Macro.prewalk(ast, &Macro.expand(&1, env))
But yeah, as seen it is easy enough to write it that it is not really needed to add it to the Macro module.
No branches or pull requests