Hellos,
All that I want to do is a basic a macro to access some arrays. Here is the working code with expressions:
# works
let
N = 5
a = zeros(N)
b = collect(1:N)
myexp = quote
for i = 1:$N
$a[i] = 2*$b[i]
end
end
eval(myexp)
a
end
Here my attempt to make a macro
# i wish it could work
macro myLoop(a, b, N)
quote
a, b, N = esc.((a,b,N))
for i = 1:$N
$a[i] = 2*$b[i]
end
end
end
function mytest()
N = 7
a = zeros(N)
b = collect(1:N)
@myLoop a b N
a
end
mytest()
I got an error that N does not exist:
ERROR: UndefVarError: N not defined
Stacktrace:
[1] mytest()
@ Main ./Untitled-1:19
[2] top-level scope
@ Untitled-1:31
I’m sure I’m doing a very basic mistake, but I can’t figure out looking at examples on discourse.
Help please