# How to cleanly initialize structure with lots of members

**URL:** <https://discourse.julialang.org/t/how-to-cleanly-initialize-structure-with-lots-of-members/102085>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [July 25, 2023, 5:43pm UTC](https://discourse.julialang.org/t/how-to-cleanly-initialize-structure-with-lots-of-members/102085 "2023-07-25T17:43:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hshackle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hshackle/32/47114_2.png) [@hshackle](https://discourse.julialang.org/u/hshackle)\
**Post date:** [July 25, 2023, 5:43pm UTC](https://discourse.julialang.org/t/how-to-cleanly-initialize-structure-with-lots-of-members/102085/1 "2023-07-25T17:43:24Z")

</div>

I have a struct with lots of different members, all of which get initialized in a standard way. Currently, my code looks like

```julia
struct A
    a::Int
    b::Int
    c::Int
    function A()
        a = 1
        b = 1
        c = 1
        new(a, b, c)
    end
end

```

which looks fine for three variables, but if I had, say, 40 of them, then the final call to `new()` starts to look very ugly and I have to be very careful about where I enter in the variables. Is there any better way to do this, for example automatically initializing the struct based on the fact that the variable names in the constructor are the same as in the struct definition?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 25, 2023, 5:48pm UTC](https://discourse.julialang.org/t/how-to-cleanly-initialize-structure-with-lots-of-members/102085/2 "2023-07-25T17:48:55Z")

</div>

The [`Base.@kwdef` macro](https://docs.julialang.org/en/v1/base/base/#Base.@kwdef) might help here. It lets you give default values to members, and initialize them with keywords.

That being said, if you have 40 members in a struct, I wonder if you might want to use some other data structure instead, something you can loop/iterate over.

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 25, 2023, 5:51pm UTC](https://discourse.julialang.org/t/how-to-cleanly-initialize-structure-with-lots-of-members/102085/3 "2023-07-25T17:51:09Z")

</div>

For such scenarios (where you have the defaults for your field values) is better to use the `@kwdef` approach (note that you no longer need to use `new()` for setting your defaults):

```julia
@kwdef struct A
    a::Int=1
    b::Int=1
    c::Int=1
    d::Int=2
end

```

This will generate a few constructors for you. Besides the default constructor you are also getting a keyword-based constructor.

Ways you can instantiate your `A` after defining your struct with `@kwdef`:

```julia
A()
A(b=3, d=5)
A(1,2,3,4)

```

---

<div class="post-metadata">

**Author:** ![hshackle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hshackle/32/47114_2.png) [@hshackle](https://discourse.julialang.org/u/hshackle)\
**Post date:** [July 25, 2023, 5:59pm UTC](https://discourse.julialang.org/t/how-to-cleanly-initialize-structure-with-lots-of-members/102085/4 "2023-07-25T17:59:00Z")

</div>

Thanks! I’ll take a look at this macro. Unfortunately, for my particular context, I think the code is ultimately more readable if all the different entries are kept separate, as all their uses are quite different. I’ll think more about it though, as I share your discomfort with such an unwieldy structure.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 25, 2023, 6:08pm UTC](https://discourse.julialang.org/t/how-to-cleanly-initialize-structure-with-lots-of-members/102085/5 "2023-07-25T18:08:43Z")

</div>

You may want to consider [GitHub - mauro3/Parameters.jl: Types with default field values, keyword constructors and (un-)pack macros](https://github.com/mauro3/Parameters.jl) which allows you write the default values inline with the fields.

> **[GitHub - mauro3/Parameters.jl: Types with default field values, keyword...](https://github.com/mauro3/Parameters.jl)**
>
> Types with default field values, keyword constructors and (un-)pack macros - GitHub - mauro3/Parameters.jl: Types with default field values, keyword constructors and (un-)pack macros

Here is the example usage:

```julia
julia> using Parameters

julia> @with_kw struct A
           a::Int = 6
           b::Float64 = -1.1
           c::UInt8
       end

julia> A(c=4)
A
  a: 6
  b: -1.1
  c: 4

julia> A()
ERROR: Field 'c' has no default, supply it with keyword.

julia> A(c=4, a = 2)
A
  a: 2
  b: -1.1
  c: 4

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 25, 2023, 9:06pm UTC](https://discourse.julialang.org/t/how-to-cleanly-initialize-structure-with-lots-of-members/102085/6 "2023-07-25T21:06:07Z")

</div>

> [@mkitti](#):
>
> You may want to consider [GitHub - mauro3/Parameters.jl: Types with default field values, keyword constructors and (un-)pack macros](https://github.com/mauro3/Parameters.jl) which allows you write the default values inline with the fields.

Note that this is also possible with `Base.@kwdef`, which removes a lot of the need for Parameters.jl these days.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 25, 2023, 10:39pm UTC](https://discourse.julialang.org/t/how-to-cleanly-initialize-structure-with-lots-of-members/102085/7 "2023-07-25T22:39:38Z")

</div>

> [@hshackle](#):
>
> but if I had, say, 40 of them, then the final call to `new()` starts to look very ugly

With that many fields, I suspect that you should group them into multiple sub-types. A single struct with 40 plain primitive fields seems a bit alien to me.
