Parameter Stores to Struct or not to Struct, what is the julian way?

I am making a simulation package in Julia. There are maybe 10-20 different parameters that must be user defined(not derivable from other means) passed around different functions(mostly in pieces but still all need to be entirely in scope at some point).

For the sake of the sanity of the end user I’d like to put these into some object: potentially a struct, maybe a Dict? Mostly for convenience, otherwise a user may have to make a function call with 10-20 different keywordless parameters. Another set of alternatives to consider could be to have a “template” with a bunch of globals, or even a UI (kind of forces the workflow of the end user though…), a csv/json type file format?

What’s the Julian way of handling these scenarios?

1 Like

Another alternative would be to have it all in a struct, and make all other functions reference objects of that type? That would be a more classic FP type design I think.

I think Parameters.jl is what you’re looking for.

2 Likes

@with_kw macro from Parameters.jl is really cool. It also defines the show methods automatically. I strongly recommend it in any case.

2 Likes

Awesome!

Thanks both of you this looks like exactly what I wanted!

1 Like

Also look at Base.@kwdef.

3 Likes

I’ve been using Parameters with named tuples, but I might use https://github.com/JuliaDiffEq/LabelledArrays.jl#relation-to-namedtuples in the future as a replacement. I like that I get vector functionality for free, and it makes all the elements the same type, which improves compilation time.

One advantage of using a struct and Parameters is that you can make use of multiple dispatch. This is useful if you have multiple models which use different sets of parameters.

2 Likes