Search code examples
juliaevalmetaprogramming

Julia local variable not defined in expression eval


Given the following function,

function floop(exp)
    a = 5
    b = 10
    ex = Expr(:call, :+, 1, exp);
    return eval(ex);
end

if I then run

floop(Symbol("b"))

I get an error saying that b is not defined. Why does this not work? How can I make this work?


Solution

  • One of the key things that lets Julia be fast is that eval always runs in the global scope. This means it can never refer to local variables.