Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Metaprogramming and variable scope (again)

$
0
0

Hi there, I’m having some troubles understanding variable scoping in macros. Here is the simplest code to show my problems

macro foo(f) 
    quote
        x = 2
        $f
    end
end

function bar(y)
    @foo sum(x*i for i in 1:y)
end

If I run this code from the REPL I get ERROR: UndefVarError: y not defined. However, if I define y in the REPL, then everything works fine. It is possible to make the above example work as it should?

4 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images