Using Symbol within function (unexpected behaviour)

eval() always operates in global scope. Check out the help text:

help?> eval
...

  eval(expr)

  Evaluate an expression in the global scope of the containing module. ...

Since eval() operates in global scope, it does not have access to the values of your local variables.

Since Julia 1.1, though, we have the Base.@locals() macro, which gives you a dictionary mapping local variable names to their local values. Your use case might be a good fit for that:

julia> function foo()
         a = 1
         println(Base.@locals())
       end
foo (generic function with 1 method)

julia> foo()
Dict{Symbol,Any}(:a=>1)
2 Likes