Consider the following function:
function func(vars::Dict{Symbol, Any})
end
I want to create local variables in a func
's scope,
where:
variable names are each the key of the vars
and
variable values are values in a vars
corresponding to the given key.
I am aware that I could use eval()
in something like:
for (k, v) in vars
eval(:($k = $v))
end
However, that will define variables in the global scope, which has performance repercussions and might override existing global variables.
P.S. I will add a context in case you have suggestions that are drastically different than the approach I am going for. I am implementing a wrapper around the JuMP allowing the users to dynamically create optimization models with arbitrary constraint expressions. Since the constraint expressions are defined using symbols, and JuMP
requires those symbols to be defined in the current scope, I want the user to provide those symbols and their values in a dictionary to the function. Defining the symbols globally would be cumbersome as the user should ideally be able to run the function multiple times with the same constraint expressions (i.e. same symbols) but different values.