I ended up building the Expr explicitly, but I’d still like to know if this
is the idiomatic way or if there’s another that’s considered “better”:
foo(N) = Expr(:call, :gradient, :A, map(k -> symbol("xs_", k), 1:N)...)
// T
On Tuesday, September 22, 2015 at 9:11:05 AM UTC+2, 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)
>
>
>