New Julia user here :)

Following the scipy/julia tutorial by D. Sanders and playing around with 
Macros 
http://nbviewer.ipython.org/github/dpsanders/scipy_2014_julia/blob/master/Metaprogramming.ipynb

He has in there an example of macros. I was playing around with that and I 
ran into a case where I don't understand why the expanded code ends up 
introducing what seems to be a local variable. And this depends solely on 
whether `expr2` contains `i += 1` or `i = i + 1`.

Can someone take the time to explain why i one case the macro expansion 
introduces a local variable? And how would one get around that in this case?

Thanks,
-z


In [111]:

macro until(expr1, expr2)

    quote

    #:(

        while !($expr1)  # code interpolation

            $expr2

        end

    #)

    end

end

 

In [122]:

expr1 = 

quote

    i = 0

    @until i==10 begin

        print(i)

        i += 1

    end    

end;

expr2 = 

quote

    i = 0

    @until i==10 begin

        print(i)

        i = i + 1

    end    

end;

In [123]:

eval(expr1)

0123456789

In [124]:

eval(expr2)

i not defined
while loading In[124], in expression starting on line 1

 in anonymous at In[122]:4


In [125]:

macroexpand(expr1)

Out[125]:

quote  # In[122], line 3:
    i = 0 # line 4:
    begin  # In[111], line 4:
        while !(i == 10) # line 5:
            begin  # In[122], line 5:
                print(i) # line 6:
                i += 1
            end
        end
    end
end

In [126]:

macroexpand(expr2)

Out[126]:

quote  # In[122], line 11:
    i = 0 # line 12:
    begin  # In[111], line 4:
        while !(#3677#i == 10) # line 5:
            begin  # In[122], line 13:
                print(#3677#i) # line 14:
                #3677#i = #3677#i + 1
            end
        end
    end
end

Reply via email to