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

Reassigning Variables within @capture #94

Open
yurivish opened this issue Aug 26, 2018 · 1 comment
Open

Reassigning Variables within @capture #94

yurivish opened this issue Aug 26, 2018 · 1 comment

Comments

@yurivish
Copy link

@capture introduces assignments to variables indicated in its second argument. When one of these assignments is to a variable with the same name as the first argument, things seem to go wrong — the match fails and the value of that variable is set to nothing. I think the better behavior in this case would be for the match to succeed, and for ex and b in the second example to have the values of a and b in the first.

julia> ex = :(1 + 2)
:(1 + 2)

julia> @capture(ex, a_ + b_)
true

julia> @capture(ex, ex_ + b_)
false

julia> ex
@MikeInnes
Copy link
Member

I agree. This just happens because of the way we expand things –

ex = MacroTools.nothing
b = MacroTools.nothing
llama = (MacroTools.trymatch)($(Expr(:copyast, :($(QuoteNode(:(ex_ + b_)))))), ex)
if llama == MacroTools.nothing
    false
else
    ex = (MacroTools.get)(llama, :ex, MacroTools.nothing)
    b = (MacroTools.get)(llama, :b, MacroTools.nothing)
    true
end

But you could straightforwardly change this to set ex = nothing only in the failure case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants