Default value of *some* fields in a mutable struct

That is awesome! Thank you Gunnar.

This is why I love Julia, hackable all the way down to the bit.

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

This pretty much is completly generic

coly_rand(x=2,y=2)
coly_rand(2, 2.0, true, 0, 2.55f-43, false)
coly_rand(x=2)
coly_rand(2, 1.1609548413287613e-28, false, 1702389026, 0.0f0, false)
2 Likes