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

**URL:** <https://discourse.julialang.org/t/how-to-check-if-field-in-a-structure-has-defined-default-value/77449>\
**Category:** General Usage\
**Tags:** question, struct\
**Created:** [March 5, 2022, 7:09pm UTC](https://discourse.julialang.org/t/how-to-check-if-field-in-a-structure-has-defined-default-value/77449 "2022-03-05T19:09:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sz1992](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sz1992/32/19219_2.png) [@sz1992](https://discourse.julialang.org/u/sz1992)\
**Post date:** [March 5, 2022, 7:09pm UTC](https://discourse.julialang.org/t/how-to-check-if-field-in-a-structure-has-defined-default-value/77449/1 "2022-03-05T19:09:24Z")

</div>

Hi,  
let’s take this structure

```julia
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

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 5, 2022, 9:14pm UTC](https://discourse.julialang.org/t/how-to-check-if-field-in-a-structure-has-defined-default-value/77449/2 "2022-03-05T21:14:36Z")

</div>

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

```julia
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…

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 6, 2022, 12:15am UTC](https://discourse.julialang.org/t/how-to-check-if-field-in-a-structure-has-defined-default-value/77449/3 "2022-03-06T00:15:42Z")

</div>

This only works with `!isbits` fields.

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 6, 2022, 12:23am UTC](https://discourse.julialang.org/t/how-to-check-if-field-in-a-structure-has-defined-default-value/77449/4 "2022-03-06T00:23:24Z")

</div>

> [@sz1992](#):
>
> `Base.@kwdef`

`Base.@kwdef` is not [exported](https://github.com/JuliaLang/julia/issues/33192) nor [documented](https://github.com/JuliaLang/julia/issues/32659) and can simply disappear between Julia version, use [Parameters.jl](https://github.com/mauro3/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.
