Hey, can anyone help with this macro expansion not working like I expect?
gensyming the local variables in a function body if the function’s signature is
escaped. Anyone seen this before? Thanks
I want to emit several functions from a macro. They all have the same function signature except for the name of the function, so I was creating that in a variable and interpolating it into the returned quoted expression.
However, I seem to have hit a problem where escaping the function signature stops the macro expander from
gensyming local variables in the function body!
Here’s a MWE:
Un-escaped, everything is fine:
julia> macro m() sig = :(f(x)) quote $(Expr(:function, sig, quote v = x + 1 v end)) end end @m (macro with 2 methods) julia> @macroexpand @m() quote #= none:3 =# function #178#f(#180#x) #= none:4 =# #179#v = #180#x + 1 #= none:5 =# #179#v end end
But if I escape the signature,
v isn’t escaped:
julia> macro m() sig = :(f(x)) quote $(Expr(:function, esc(sig), quote v = x + 1 v end)) end end @m (macro with 2 methods) julia> @macroexpand @m() quote #= none:3 =# function f(x) #= none:4 =# Main.v = Main.x + 1 #= none:5 =# Main.v end end
Any ideas? Is this a bug in the macro expander?
Also, weirdly, it works if I don’t wrap the returned expression in a
quote block, and just return the function expression directly, but I think I need the quote block because i have three functions to return, not just one?