[Probably] A Naive Macro Interpolation Doubt

Here’s the problem–your macro is producing code which tries to call the esc function on some local variable named N which doesn’t exist. esc is just like any other function–it doesn’t know that you actually want to interpolate the value of N (that’s what the $ syntax is for).

Here’s one version that works:

julia> macro myLoop(a, b, N)
           quote
               for i = 1:$(esc(N))
                   $(esc(a))[i] = 2*$(esc(b))[i]
               end
           end
       end

or, if you prefer:

julia> macro myLoop(a, b, N)
           quote
               a, b, N = ($(esc(a)), $(esc(b)), $(esc(N)))
               for i = 1:N
                   a[i] = 2*b[i]
               end
           end
       end

1 Like