# Add new field to struct?

**URL:** https://discourse.julialang.org/t/add-new-field-to-struct/73923
**Category:** General Usage
**Tags:** struct
**Created:** [January 2, 2022, 10:57am UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923 "2022-01-02T10:57:34Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [January 2, 2022, 10:57am UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/1 "2022-01-02T10:57:34Z")

</div>

Hello!

Is there any way to add a field to an already defined struct? I have a case where I have no idea of how many fields I will need, so I would like to be able to do something similar to Matlab’s structs i.e:

```julia
MatlabStruct.NewField = "SomeValue"

```

Which will not work in Julia if “NewField” is not already defined in the struct. I would like to be able to do this some way since I prefer struct syntax much more than “Dicts” due to the “.” syntax instead of [“”], which gets quite cumbersome quickly (in my own opinion).

If it is possible to do what I want, I would like to know - if it is still not possible that is of course also an answer.

Kind regards

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [January 2, 2022, 11:27am UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/2 "2022-01-02T11:27:57Z")

</div>

It’s not possible to dynamically add fields to a `struct` in Julia for a variety of mostly performance related reasons. However, if it’s only a question of syntax, dot overloading is available like this:

```julia
struct Foo
    properties::Dict{Symbol, Any}
end
Foo() = Foo(Dict{Symbol, Any}())

Base.getproperty(x::Foo, property::Symbol) = getfield(x, :properties)[property]
Base.setproperty!(x::Foo, property::Symbol, value) = getfield(x, :properties)[property] = value
Base.propertynames(x::Foo) = keys(getfield(x, :properties))

```

Now you can access the internal dictionary with dot syntax.

```julia
julia> x = Foo()
Foo(Dict{Symbol, Any}())

julia> x.a = 1
1

julia> x.a
1

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [January 2, 2022, 11:38am UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/3 "2022-01-02T11:38:37Z")

</div>

Thanks for that Gunnar!

I have been searching for a while and just today I stumbled upon this: [https://github.com/aminya/VarStructs.jl](https://github.com/aminya/VarStructs.jl)

Which seems to do what I actually want, even though it might not be best practice as you mention. Using a dict with the syntax you propose also seem like a good option, but that would mean I need to implement the Base get/set property for every custom Dict I define I suppose?

Now I am a bit closer to my goal none the less so thank you very much!

Kind regards

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [January 2, 2022, 11:44am UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/4 "2022-01-02T11:44:57Z")

</div>

Depending on what stuct features you want, NamedTuples may be a good fit:

```julia
x = (field_a = 1, b = "some string")
x = (; x..., field_c = 123.45)

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [January 2, 2022, 11:47am UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/5 "2022-01-02T11:47:40Z")

</div>

> [@aplavin](#):
>
> ```julia-auto
> x = (field_a = 1, b = "some string")
> x = (; x..., field_c = 123.45)
> 
> ```

Interesting usage of NamedTuples, I have not seen that before. Not what I want in this case though, thank you for teaching me something new still!

Kind regards

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 2, 2022, 12:13pm UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/6 "2022-01-02T12:13:57Z")

</div>

Doesn’t the `VarStructs.jl` package basically use the same idea as @GunnarFarneback? That is, it uses a Dict to map field names to field properties? Thank you.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [January 2, 2022, 12:32pm UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/7 "2022-01-02T12:32:20Z")

</div>

I just looked at the package and yes, it uses the same idea. It does add a couple of bells and whistles around it, most importantly a macro to create such `struct`s and support for handling unset values and enforce types of the values. It also adds a bit of illusion that it actually creates a `struct` with the given fields rather than a wrapped `Dict`, but that won’t convince type inference. As long as you don’t expect more than it actually provides it should be fine to use it.

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [January 2, 2022, 12:58pm UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/8 "2022-01-02T12:58:50Z")

</div>

I will be using it mainly for a tool which allows a user to make basically a text document, so it should be fine, thanks! Especially since top performance is not a priority in the first write of the code, trying to go for convenience this time.

Could you explain the point with type inference with a bit more detail? Basically my understanding from your reply is that even though it appears as struct to me when working with it, the Julia compiler might not regard it as such and not be able to optimize it? I like to learn.

Kind regards

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 2, 2022, 4:20pm UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/10 "2022-01-02T16:20:08Z")

</div>

If I am not mistaken, VarStructs are always mutable thus we cannot benefit from the performance of immutable structures?

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [January 2, 2022, 4:39pm UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/11 "2022-01-02T16:39:30Z")

</div>

This is a common question (one that I asked before also). There are a few ways to change structs when prototyping. Solutions range from Named Tuples (like above) to something like the following:

```julia
struct Foo1() 
   # fieldnames 
end
const Foo = Foo1

```

and then when you want to change the fields

```julia
struct Foo2() 
   # add new fields
end
const Foo = Foo2() # rebind 

```

I think the above is suggested by `Revise.jl` but can’t find it in their documentation.  
You can also use [ProtoStructs · Julia Packages](https://juliapackages.com/p/protostructs) or [RedefStructs · JuliaHub](https://juliahub.com/ui/Packages/RedefStructs/nrk8Q/0.1.2)

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [January 2, 2022, 4:50pm UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/12 "2022-01-02T16:50:19Z")

</div>

Thanks for your input! Nice to know these packages exist.

In my case it was not about prototyping, but making the data structure actually behave like a structure and being able to add fields at will. Here VarStructs.jl has proven quite useful.

The only thing which annoys me using VarStructs is that the auto-completion (i.e. TAB in REPL) does not show all the fields connected to the struct anymore. Wondering if there is any way to change that?

Kind regards

---

<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: [January 7, 2022, 2:06pm UTC](https://discourse.julialang.org/t/add-new-field-to-struct/73923/13 "2022-01-07T14:06:03Z")

</div>

> [@Ahmed\_Salih](#):
>
> he only thing which annoys me using VarStructs is that the auto-completion (i.e. TAB in REPL) does not show all the fields connected to the struct anymore. Wondering if there is any way to change that?

Hi!

This is because VarStructs.jl does not implement `Base.propertynames()` for the defined structures. If you add an implementation for this function as well, as @GunnarFarneback suggested, the Julia REPL will pick that up and autocomplete your field names. HTH!

That being said, you could just use a `Dict` with `Symbol` keys instead of tweaking structs for something which they were not really designed. In other words, do not add all that boilerplate to hide the behavior behind a struct-like interface, simply use the `Dict` API directly instead. All the more since this will make reading your code easier for others, who will probably not expect structs with dynamic fields.
