What you're trying isn't easy because the results of @ntuple, unless contained inside an Expr, will be eval'ed so you'll still create the tuple. And expression-tuples are not iterable, so you can't splat them.
Manual construction: function foo(N) args = [symbol("xs_",k) for k = 1:N] :(gradient(A, $(args...))) end The Cartesian package had an `@ncall`, but I found that I so often wanted new combinations of arguments that it wasn't worth it so didn't port it over to Base. --Tim On Tuesday, September 22, 2015 12:11:05 AM Tomas Lycken wrote: > 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) > >