Reputation: 5756
I have different set of math expressions that must be evaluated at run-time. Currently the task is done by replacing symbols with equivalent values and eval
the result. (could be done by any existing symbolic packages)
Now, refer to the definition of modules in Julia-lang:
Modules in Julia are separate variable workspaces, i.e. they introduce a new global scope .... Modules allow you to create top-level definitions (aka global variables) without worrying about name conflicts when your code is used together with somebody else’s.
And with the power of Julia to do meta-things,
I'm wondering if it is possible to create anonymous modules at run-time m=Module()
, and use them as a scope to evaluate expressions eval(m, :(a+b))
.
But I simply can't find a way to load variable into the run-time modules.
Although I could get result with:
julia> ex=:(module mo; a=1; b=4; end)
julia> eval(ex)
julia> eval(mo,:(a+b))
I prefer more functional way, using anonymous modules.
Any Help.
Upvotes: 3
Views: 87
Reputation: 466
This works:
julia> m=Module()
anonymous
julia> eval(m, :(a=5))
5
julia> m.a
5
julia> eval(m, :(a))
5
julia> eval(m, :(2a))
10
Upvotes: 5