I’m having real difficulties grok-ing macro hygiene. I’ve read the manual 
entry, but didn’t get much wiser, so I’m hoping the solution to my current 
problem will be another data point I can use to understand (and I need this 
to work :P).

I have a utility function that builds an expression (which will later be 
interpolated into the method body of a generated function, but that’s out 
of scope for my current problem), something like this:

julia> using Base.Cartesian

julia> foo(N) = :(gradient(A, (@ntuple $N k->xs_k)...))
foo (generic function with 1 method)

julia> macroexpand(foo(2))
:(gradient(A,(xs_1,xs_2)...))

Now, as you see, the generated tuple is splatted at run time. I’d like to 
try to move that splatting to compile time, so that the generated 
expression is rather :(gradient(A, xs_1, xs_2)). I’ve tried variants on the 
following, but as you can see I haven’t had any success.

I assume that I need to escape N somehow - but how?

julia> foo(N) = :(gradient(A, $((@ntuple N k->xs_k)...)))
ERROR: MethodError: `_ntuple` has no method matching _ntuple(::Symbol, ::Expr)

julia> foo(N) = :(gradient(A, $((@ntuple $N k->xs_k)...)))
ERROR: MethodError: `_ntuple` has no method matching _ntuple(::Expr, ::Expr)

julia> foo(N) = :(gradient(A, $(esc((@ntuple N k->xs_k)...))))
ERROR: MethodError: `_ntuple` has no method matching _ntuple(::Symbol, ::Expr)

julia> foo(N) = :(gradient(A, $(esc((@ntuple :N k->xs_k)...))))
ERROR: MethodError: `_ntuple` has no method matching _ntuple(::Expr, ::Expr)

julia> foo(N) = :(gradient(A, $(@ntuple esc(N) k->xs_k)...))
ERROR: MethodError: `_ntuple` has no method matching _ntuple(::Expr, ::Expr)

julia> foo(N) = :(gradient(A, $(@ntuple esc(:N) k->xs_k)...))
ERROR: MethodError: `_ntuple` has no method matching _ntuple(::Expr, ::Expr)

​

Reply via email to