Skip to content
New issue

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? Sign in to your account

Macro expansion bug? #378

Closed
yrashk opened this issue Jul 4, 2012 · 9 comments
Closed

Macro expansion bug? #378

yrashk opened this issue Jul 4, 2012 · 9 comments

Comments

@yrashk
Copy link
Contributor

@yrashk yrashk commented Jul 4, 2012

iex> expr = quote do
...> if !a, do: oneway, else: anotherway
...> end
{:if,0,[{:"!",0,[{:a,0,:quoted}]},[{:do,{:oneway,0,:quoted}},{:else,{:anotherway,0,:quoted}}]]}
iex> IO.puts Macro.to_binary(Macro.expand(expr, __ENV__))  
case(!!a) do
  false ->
    oneway
  true ->
    anotherway
end
:ok
iex> Macro.expand(expr, __ENV__)
{:case,0,[{:"!",0,[{:"!",0,[{:a,0,:quoted}]}]},[{:do,{:"->",0,[{[false],{:oneway,0,:quoted}},{[true],{:anotherway,0,:quoted}}]}}]]}
@josevalim
Copy link
Member

@josevalim josevalim commented Jul 4, 2012

What is supposed to be the bug here? Notice that Macro.expand does not expand recursively.

@yrashk
Copy link
Contributor Author

@yrashk yrashk commented Jul 4, 2012

I guess what I was looking for is recursive macro expansion... anyway, why does it expand ! to !! ?

@josevalim
Copy link
Member

@josevalim josevalim commented Jul 4, 2012

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.

@josevalim josevalim closed this Jul 4, 2012
@yrashk
Copy link
Contributor Author

@yrashk yrashk commented Jul 4, 2012

No way we can support optional recursive expansion? Doesn't make much sense?

@josevalim
Copy link
Member

@josevalim josevalim commented Jul 4, 2012

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.

@jechol
Copy link

@jechol jechol commented Oct 12, 2017

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.

@jechol
Copy link

@jechol jechol commented Oct 12, 2017

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...

@josevalim
Copy link
Member

@josevalim josevalim commented Oct 12, 2017

Macro.expand recurses but it does not traverse.

@OvermindDL1
Copy link
Contributor

@OvermindDL1 OvermindDL1 commented Oct 12, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.