Structures With Properties

Although properties can be used flexibly with different structures, it may be easier to take advantage of the provided AbstractMetadata type. In the following example we take advantage of the Description and DictExtension. These provide a method of describing a structure and an extensible pool for storing an arbitrary number of properties.

julia> mutable struct MyProperties{M} <: AbstractMetadata{M}

Binding Description and DictExtension to specific fields is accomplished through @assignprops. Several other methods specific to MyProperties are created to provide property like behavior. Most notably, the methods from base overwritten are getproperty, setproperty!, and propertynames.

julia> @properties MyProperties begin
           description(x) => :my_description
           description!(x, val) => :my_description
           Any(x) => :my_properties
           Any!(x, val) => (:my_properties)

julia> m = MyProperties("", Dict{Symbol,Any}())
MyProperties{Dict{Symbol,Any}} with 1 entry

julia> propertynames(m)
julia> description(m)

julia> description!(m, "foo")
MyProperties{Dict{Symbol,Any}} with 1 entry
    description: foo

julia> description(m)

julia> m.description = "bar"

julia> description(m)

julia> m.description

Optional properties can be assigned to the DictExtension using the DictExtension(Propert1, Property2) syntax.

julia> @defprop CalibrationMaximum{:calmax}

julia> propdefault(::CalibrationMaximumType, x::AbstractArray) = maximum(x)

julia> proptype(::CalibrationMaximumType, ::Type{<:AbstractArray{T,N}}) where {T,N} = T

julia> @defprop CalibrationMinimum{:calmin}

julia> propdefault(::CalibrationMinimumType, x::AbstractArray) = minimum(x)

julia> proptype(::CalibrationMinimumType, ::Type{<:AbstractArray{T,N}}) where {T,N} = T

julia> struct MyArray{T,N,P<:AbstractArray{T,N},M<:AbstractDict{Symbol,Any}} <: AbstractArray{T,N}

julia> Base.parent(m::MyArray) = getfield(m, :_parent)

julia> Base.size(m::MyArray) = size(parent(m))

julia> Base.maximum(m::MyArray) = maximum(parent(m))

julia> Base.minimum(m::MyArray) = minimum(parent(m))

julia> @assignprops(
           :my_properties => dictextension(calmax,calmin))