You probably don't want `eval` unless there's no other way. It's hard to
tell how you want to use the variables, so it's hard to recommend
alternatives. Keyword arguments can be useful for this sort of thing:

function f(; a = 1, b = 2)
    a + b
end

f(a = 99, b = 2)

You can also use Dict's to pass keyword arguments into functions as
follows:

using Compat
d = @compat Dict(:a => 3, :b => 4)

f(;d...)

Creating a type that holds the parameters is another good option, depending
on how the parameters will be used.


On Wed, Feb 11, 2015 at 3:25 PM, David P. Sanders <[email protected]>
wrote:

> Hi,
>
> If I have a dictionary
>
> params = {"N": 10, "M": 2.0}
>
> how can I use it to define two variables N and M with the corresponding
> values?
>
> This sounds like it should be easy and obvious, say using `eval`?
> E.g. extract the keys and values into strings and then use
>
> eval(parse("N=10"))
>
> Is this reasonable?
>
> The use case is to load input parameters for a simulation. Maybe there is
> a better way?
>
> Thanks,
> David.
>

Reply via email to