API

Parameters.ParametersModule

This is a package I use to handle numerical-model parameters, thus the name. However, it should be useful otherwise too. It has two main features:

  • keyword type constructors with default values, and
  • unpacking and packing of composite types and dicts.

The macro @with_kw which decorates a type definition to allow default values and a keyword constructor:

julia> using Parameters

julia> @with_kw struct A
           a::Int = 6
           b::Float64 = -1.1
           c::UInt8
       end

julia> A(c=4)
A
  a: 6
  b: -1.1
  c: 4

Unpacking is done with @unpack (@pack! is similar):

struct B
    a
    b
    c
end
@unpack a, c = B(4,5,6)
# is equivalent to
BB = B(4,5,6)
a = BB.a
c = BB.c
source
Parameters.reconstructMethod
reconstruct(pp; kws...
reconstruct(T::Type, pp; kws...)

Make a new instance of a type with the same values as the input type except for the fields given in the keyword args. Works for types, Dicts, and NamedTuples. Can also reconstruct to another type, which is probably mostly useful for parameterised types where the parameter changes on reconstruction.

Note: this is not very performant. Check Setfield.jl for a faster & nicer implementation.

julia> using Parameters

julia> struct A
           a
           b
       end

julia> x = A(3,4)
A(3, 4)

julia> reconstruct(x, b=99)
A(3, 99)

julia> struct B{T}
          a::T
          b
       end

julia> y = B(sin, 1)
B{typeof(sin)}(sin, 1)

julia> reconstruct(B, y, a=cos) # note reconstruct(y, a=cos) errors!
B{typeof(cos)}(cos, 1)
source
Parameters.type2dictMethod

Transforms a type-instance into a dictionary.

julia> struct T
           a
           b
       end

julia> type2dict(T(4,5))
Dict{Symbol,Any} with 2 entries:
  :a => 4
  :b => 5

Note that this uses getproperty.

source
Parameters.with_kwFunction

This function is called by the @with_kw macro and does the syntax transformation from:

@with_kw struct MM{R}
    r::R = 1000.
    a::R
end

into

struct MM{R}
    r::R
    a::R
    MM{R}(r,a) where {R} = new(r,a)
    MM{R}(;r=1000., a=error("no default for a")) where {R} = MM{R}(r,a) # inner kw, type-paras are required when calling
end
MM(r::R,a::R) where {R} = MM{R}(r,a) # default outer positional constructor
MM(;r=1000,a=error("no default for a")) =  MM(r,a) # outer kw, so no type-paras are needed when calling
MM(m::MM; kws...) = reconstruct(mm,kws)
MM(m::MM, di::Union{AbstractDict, Tuple{Symbol,Any}}) = reconstruct(mm, di)
macro unpack_MM(varname)
    esc(quote
    r = varname.r
    a = varname.a
    end)
end
macro pack_MM(varname)
    esc(quote
    varname = Parameters.reconstruct(varname,r=r,a=a)
    end)
end
source
Parameters.@with_kwMacro

Macro which allows default values for field types and a few other features.

Basic usage:

@with_kw struct MM{R}
    r::R = 1000.
    a::Int = 4
end

For more details see manual.

source
Parameters.@with_kw_noshowMacro

As @with_kw but does not define a show method to avoid annoying redefinition warnings.

@with_kw_noshow struct MM{R}
    r::R = 1000.
    a::Int = 4
end

For more details see manual.

source