I have a macro: @m1 which takes args... as argument as in:
macro m1(args...)
...
end
I wanted to create e new macro @m2 which calls @m1 with an extra parameter.
That is, I want @m2(a[1:3] = 3, key = :b) to be equivalent to @m1(a[1:3] = 3, key = :b, MyType())
The best I could do was:
macro m2(args..)
return :(@m1($(args...), MyType()))
end
But as expected I have problem with scoping (I need to use esc…).
Unfortunately, things like:
macro m2(args..)
return :(@m1($(esc(args)...), MyType()))
end
fail…
It look so close to the final solution, but it fails…
Any ideas?
Isn’t this just an issue with your use of esc on a tuple without broadcasting?
This seems to work fine (even if @m1 is in another module):
module A
export @m1
macro m1(args...)
quote
println($(esc.(args)...))
end
end
end # module A
macro m2(args...)
quote
@m1($(esc.(args)...), 4)
end
end
julia> using .A
julia> @macroexpand @m1 1 2 3
quote
#= REPL[1]:5 =#
(Main.A.println)(1, 2, 3)
end
julia> @macroexpand @m2 1 2 3
quote
#= REPL[2]:3 =#
begin
#= REPL[1]:5 =#
(Main.A.println)(1, 2, 3, 4)
end
end
and it seems behave correctly from an hygiene point of view:
julia> let x = 3
@macroexpand @m2 1 2 x
end
quote
#= REPL[2]:3 =#
begin
#= REPL[1]:5 =#
(Main.A.println)(1, 2, x, 4)
end
end
julia> let x = 3
@m2 1 2 x
end
1234