# Adding new field to existing structure

**URL:** <https://discourse.julialang.org/t/adding-new-field-to-existing-structure/99117>\
**Category:** General Usage\
**Created:** [May 19, 2023, 1:35pm UTC](https://discourse.julialang.org/t/adding-new-field-to-existing-structure/99117 "2023-05-19T13:35:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [May 19, 2023, 1:35pm UTC](https://discourse.julialang.org/t/adding-new-field-to-existing-structure/99117/1 "2023-05-19T13:35:42Z")

</div>

I am trying to learn more about the way to organize my Julia code. I know from the documentation that I can create a structure to hold some data like this:

```julia
## The structure below has lots of fields, I am just showing one as example
struct Person
nArms::Int64
... 
end
 
p = Person(2)
println("Number of arms = ", p.nArms)

```

I would like to learn how to add a new field to the existing structure created above:

```julia
 nLegs = 2

```

Such that I can now have a new variable:

```julia
pritln("Number of legs = ", p.nLegs)

```

If I create data during my code execution, is there any way that I can add it to an existing structure that does not have a field created for it? Or should I carry the new data using variables throughout the code (without a structure definition)?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 19, 2023, 1:49pm UTC](https://discourse.julialang.org/t/adding-new-field-to-existing-structure/99117/2 "2023-05-19T13:49:06Z")

</div>

No, you can´t add new fields to a struct. If you need that kind of flexibility, probably you want a Dict.

```julia
julia> d = Dict{String,Int}("number_of_arms"=>5, "number_of_legs"=>6)
Dict{String, Int64} with 2 entries:
  "number_of_arms" => 5
  "number_of_legs" => 6

julia> d["number_of_arms"]
5

julia> d["number_of_heads"] = 4
4

julia> d
Dict{String, Int64} with 3 entries:
  "number_of_arms" => 5
  "number_of_heads" => 4
  "number_of_legs" => 6

```

(there are some alternatives, like using named tuples, but be careful anyway, this kind of flexibility can cause important performance problems:

```julia
julia> t = (nArms=1, nLegs=4)
(nArms = 1, nLegs = 4)

julia> t = (t..., nHeads = 5)
(nArms = 1, nLegs = 4, nHeads = 5)

julia> t.nHeads
5

```

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [May 19, 2023, 1:54pm UTC](https://discourse.julialang.org/t/adding-new-field-to-existing-structure/99117/3 "2023-05-19T13:54:25Z")

</div>

If you need to add fields dynamically to your data, then you probably need a `Dict` instead of a `struct`.

E.g.,

```julia
> const Person = Dict{Symbol, Any}

> p1 = Person(:nArms => 2)
Dict{Symbol, Any} with 1 entry:
  :nArms => 2

> p1[:nLegs] = 2; p1
Dict{Symbol, Any} with 2 entries:
  :nLegs => 2
  :nArms => 2

```

If you want to have some mandatory as well as some optional fields, then you need to create an explicit constructor, or a `struct` with explicitly accessor methods (see `Base.getproperty`, `Base.setproperty!`) for details. E.g.,

```julia
> person(; nArms = 2) = Person(:nArms => nArms)

> p2 = person(nArms = 3)
Dict{Symbol, Any} with 1 entry:
  :nArms => 3

```
