I'm trying to escape a collection of symbols so that way I get a collection of the variables, but am running into issues. Here's an MWE:
macro escape_all(x...)
:($(esc.(x))...)
end
x = 1
y = 2
z = 3
macroexpand(:(@escape_all x y z))
This returns
:(((:($(Expr(:escape, :x))), :($(Expr(:escape, :y))), :($(Expr(:escape, :z))))...,))
but what I'm looking for it to return is just
(x,y,z)
Calling Expr
explicitely works:
julia> macro escape_all(xs...)
Expr(:tuple, esc.(xs)...)
end
@escape_all (macro with 1 method)
julia> @macroexpand @escape_all x y z
:((x, y, z))
But you can also use the following unquote syntax in a context where list-splicing (like ,@
in Lisp, I guess) makes sense:
julia> macro escape_list(xs...)
:([$(esc.(xs)...)])
end
@escape_list (macro with 1 method)
julia> macro escape_f(xs...)
:(f($(esc.(xs)...)))
end
@escape_f (macro with 1 method)
julia> @macroexpand @escape_list x y z
:([x, y, z])
julia> @macroexpand @escape_f x y z
:((Main.f)(x, y, z))
Funnily, I never saw $(x...)
to be talked about anywhere. I stumbled upon it recently reading someone's code. But it's mentioned in the current "latest" docs as splatting interpolation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With