The key here is that you want your macro to return an expression, not
necessarily do the computation itself. So instead of

macro my(exp)
    for i in exp.args
        println("the arg is ", eval(i))
    end
end

You want something like:

macro my(exp)
    quote
        for i in $(exp.args)
            println("the arg is ", i)
        end

    end
end

Which gives you:

In  [21]: @my quote
       a = 2
       b  = 3
    end
the arg is begin  # In[21], line 2:
    a = 2 # line 3:
    b = 3
end

​

On Tue, Sep 9, 2014 at 12:42 PM, <[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
>
>

Reply via email to