So, currently, your @my macro is evaluating the arguments in the macro
context (vs. returning them to be evaluated in the calling context.
~~~
julia> macro my2(exp)
quote
for i in $exp.args
println("the arg is ", i)
end
end
end
julia> @my2 e
the arg is # none, line 2:
the arg is a = 2
the arg is # line 3:
the arg is b = 3
~~~
Note that I'm returning a quote block from the macro, and passing in `e`,
not `:e` when I call it. Your macro is only working at it is because the
macro context and the calling context are the same (in the REPL).
Have you seen the macroexpand function? It's often very useful when
debugging macros.
~~~
julia> macroexpand(quote @my2 e end)
quote # none, line 1:
begin # none, line 3:
for #168#i = e.args # line 4:
println("the arg is ",#168#i)
end
end
end
julia> macroexpand(quote @my :e end)
the arg is begin # none, line 2:
a = 2 # line 3:
b = 3
end
quote # none, line 1:
nothing
end
~~~
-- Leah
On Tue, Sep 9, 2014 at 11:42 AM, <[email protected]> wrote:
> Just puzzling over this simple problem I'm having while learning about
> macros. Here's an expression:
>
> julia> e = quote
> a = 2
> b = 3
> end
>
> quote # none, line 2:
> a = 2 # line 3:
> b = 3
> end
>
>
> If I go through this simply, I'll get a crack at each element of the args
> array:
>
>
>
> julia> for i in e.args
> println("the arg is ", i)
> end
>
>
>
> the arg is # none, line 2:
> the arg is a = 2
> the arg is # line 3:
> the arg is b = 3
>
>
> If I try to write a macro:
>
>
> julia> macro my(exp)
> for i in exp.args
> println("the arg is ", eval(i))
> end
> end
>
> and call it like this:
>
>
> julia> @my :e
> the arg is begin # none, line 2:
> a = 2 # line 3:
> b = 3
> end
>
>
> it does all the elements at once.
>
> It's probably a simple thing, but I could do with a hint!
>
> cheers
>
>