On Wed, Jan 27, 2016 at 7:41 PM, Fady Shoukry <[email protected]> wrote:
> Hi everyone,
>
> As the title suggests, I am trying to create local variables to a function
> for a supplied dictionary argument. The Dict would be of type {Symbol, Any}
> and the goal is to create a variable for each symbol that has the
> corresponding value. I am aware that I could use eval in something like:
>
> for (k, v) in dict
>     eval(:($k = $v))
> end
>
> However, eval defined its variables in the global scope which has
> performance repercussions if I understand correctly, so I am trying to avoid
> that.

The reason you can do what you want in global scope is the reason
global scope has performance issues and is also why what you want to
do is not allowed in non-global scope, i.e. there's no way you can add
a variable programatically in a local scope and it's very unlikely we
will have this feature in the foreseeable future.

Depending on what API JuMP provide and what API you want to provide, I
think you should either keep the dict (if JuMP can handle it) or use
meta programing to construct an AST/function based on the user input
and evaluate that.

>
> I will add a bit of context in case you have suggestions that are
> drastically different than the path I am going for. I am doing this as I am
> implementing a wrapper around the JuMP JuliaOpt module 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.
>
> I appreciate your help!

Reply via email to