[julia-users] Metaprogramming and function scope
Why can't I parse a function evaluation of a function defined within the scope of another function? i.e., the following terminates with an UndefVarError: bar not defined: *function* foo() *function* bar() x end *return* eval(parse("bar()")) end x = 7 foo() However, I can do *function* foo() *function* bar() x end *return* bar() end x = 7 foo() and *function* foo() *return* eval(parse("bar()")) end *function* bar() x end x = 7 foo() Many thanks! -Pieterjan
Re: [julia-users] Metaprogramming and function scope
`eval` operates in global scope only: http://docs.julialang.org/en/release-0.4/manual/metaprogramming/#eval-and-effects On Tue, Nov 24, 2015 at 1:12 PM, Pieterjan Robbewrote: > Why can't I parse a function evaluation of a function defined within the > scope of another function? > i.e., the following terminates with an UndefVarError: bar not defined: > > *function* foo() > *function* bar() > x > end > *return* eval(parse("bar()")) > end > > x = 7 > foo() > > However, I can do > > *function* foo() > *function* bar() > x > end > *return* bar() > end > > x = 7 > foo() > > and > > *function* foo() > *return* eval(parse("bar()")) > end > > *function* bar() > x > end > > x = 7 > foo() > > Many thanks! > -Pieterjan >
Re: [julia-users] Metaprogramming and function scope
That makes sense :) Is there a workaround? I need to define some (global constant) variables, Z1, Z2, Z3 etc. (that's where the parsing comes from) by calling a function (bar) that does something with data defined in foo(). I'd like to keep this inside a single function, since it's the initialization step of a more complex simulation.