Default value of *some* fields in a mutable struct

Are you looking for something like this?

mutable struct coly_xy
    x :: Int32
    y :: Float64
    z :: Bool
    t :: Int32
    u :: Float32
    v :: Bool
    function coly_xy(;kwargs...)
        K = new()
        for (key, value) in kwargs
            setfield!(K, key, value)
        end
        return K
    end
end

It allows you to specify the fields you want to set by name.

julia> coly_xy(t=Int32(13), v=true)
coly_xy(1, 1.0e-323, false, 13, 3.9061288f-19, true)

For more convenience you may want to add a convert call if the type doesn’t match.

8 Likes