Hi Julians,
The question relates to Symbols and metaprogramming, been through the docs many times but still getting my head around it.
I’ve been struggling to pinpoint a bug in my code for the last 2 days and at last I’ve reduced it to this minimal example, can anyone help me understand what’s going on please? (In case it matters, Julia v0.6.3 on Windows 7.)
This code runs fine:
foo = :foo
@eval ($println)($foo)
> foo
and this too:
foo = :foo
function maintest()
@eval ($println)($foo)
end
maintest()
> foo
But when I define foo in the function:
function maintest()
foo = :foo
@eval ($println)($foo)
end
maintest()
I get:
ERROR: LoadError: UndefVarError: foo not defined
As a sanity check I did the following, which works as expected:
function maintest()
foo = "bar"
@eval ($println)($foo)
end
maintest()
> bar
So my question is, why do I get the error when defining the symbol in the function’s hard local scope?
Thanks