How to check if field in a structure has defined default value?

Hi,
let’s take this structure

Base.@kwdef mutable struct js1
    name::String 
    id::Int32 = 16
end

Is there a way to check and find out that field id has default value?
As an folow up question, is there a way to find out what is this default value?

Simon

1 Like

As far as I recently learned you can do this with undef like

struct Test
    f1
    f2
    Test() = new(0)
end

t = Test()
@show isdefined(t, :f2)

I don’t know about a similar mechanism for defaulted fields…

This only works with !isbits fields.

julia> struct Test
           f1 :: Int
           f2 :: Vector{Int}
           Test() = new()
       end

julia> t = Test()
Test(0, #undef)

julia> @show isdefined(t, :f1)
isdefined(t, :f1) = true
true

julia> @show isdefined(t, :f2)
isdefined(t, :f2) = false
false
2 Likes

Base.@kwdef is not exported nor documented and can simply disappear between Julia version, use Parameters.jl or a similar package instead.

I do not believe there is a programmatic way to get which fields have default values. I do believe Base.@kwdef simply creates the an adequate constructor function, so this information is not stored anywhere.