This is for what I put https://github.com/mauro3/Parameters.jl
together.  Give it a spin!

- allows fields with default values
- constructors for constructing new instances from old ones with some
  changes
- packing and unpacking macros to save on typing

Example:

using Parameters

@with_kw immutable PhysicalPara{R}
    rw::R = 1000.
    ri::R = 900.
end

# create an instance with the defaults
pp = PhysicalPara{Float64}()
# make one afresh with some non-defaults
pp2 = PhysicalPara{Float32}(ri=87)
# make another one based on the previous one with some modifications
pp3 = PhysicalPara(pp2; rw=100.)

# unpacking and packing
function fn(var, pa::PhysicalPara)
    @unpack pa: ri, rw # creates local variable ri=pa.ri and rw=pa.rw
    out = var + rw
    ri = 77.77
    @pack pa: ri # pa.ri = ri
    return out, pa
end


On Mon, 2015-11-23 at 14:32, Maxim Berman <[email protected]> wrote:
> Hello,
>
> In Matlab, people often use structures to pass around arguments between
> functions, for example problem instances. This allows some flexibility in
> the development, since I don't have to think of all variables that I need
> and their types, and new objects can be easily added to existing structs
>
> In Julia, I tend to use Dicts to replicate this behavior, to pass around
> options and helper structures to my functions. I don't think this is
> recommended since it doesn't allow functions to specialize on the type of
> objects contained in the Dict.
>
> Should I use custom types instead? If some fields can be of different
> types, should I use an abstract type for my options and then use different
> subtypes ? This seems a bit too complicated... On the other hand, writing
> down all arguments in functions without using Dicts or custom types can be
> tedious when they are a lot of variables...
>
> Thanks for your advice.

Reply via email to