# Optional field in struct

**URL:** <https://discourse.julialang.org/t/optional-field-in-struct/73338>\
**Category:** General Usage\
**Tags:** bson, struct\
**Created:** [December 19, 2021, 5:25pm UTC](https://discourse.julialang.org/t/optional-field-in-struct/73338 "2021-12-19T17:25:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Handam](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Handam](https://discourse.julialang.org/u/Handam)\
**Post date:** [December 19, 2021, 5:25pm UTC](https://discourse.julialang.org/t/optional-field-in-struct/73338/1 "2021-12-19T17:25:23Z")

</div>

Hi,  
if I have defined a struct, e.g.

```julia
mutable struct A
    a::Tuple
end

```

and then saved an object of this struct with BSON

```julia
o = A((1,2))
BSON.@save "o.bson" o

```

is it then possible to add “optional” fields to the struct such that I can still load `o.bson` without an error? Because my struct has changed over time and I don’t want to always create a new struct with a new name. So e.g. the new struct could look something like this:

```julia
mutable struct A
    a::Tuple
    b::Bool
end

```

and `BSON.@load "o.bson" o` obviously results in an error. Whether the reloaded old object has the new field with some default value or doesn’t have the new field at all is irrelevant for my use case. I am thankful for any advice how to address this problem!

---

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [December 20, 2021, 8:53pm UTC](https://discourse.julialang.org/t/optional-field-in-struct/73338/2 "2021-12-20T20:53:06Z")

</div>

You cannot redefine type in Julia, this would make optimisation impossible, the compiler would need to dynamically check what is it dealing with all the time.

If you save data as one type and want to load is as something else, the best would be to make a explicit conversion. Renaming seems to be asking for trouble.

---

<div class="post-metadata">

**Author:** ![MartinBonde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinbonde/32/33200_2.png) [@MartinBonde](https://discourse.julialang.org/u/MartinBonde)\
**Post date:** [December 20, 2021, 9:22pm UTC](https://discourse.julialang.org/t/optional-field-in-struct/73338/3 "2021-12-20T21:22:04Z")

</div>

Have you looked at [Parameters.jl](https://github.com/mauro3/Parameters.jl)?  
It makes it easy define structs with optional keyword arguments, i.e. default values for fields that are not explicitly given.  
For example b could default to false if no value is given.  
I have not used BSON before, but the following example appears to work:

```julia
using BSON
mutable struct A
  a::Tuple
end
o = A((1, 2))
BSON.@save "o.bson" o

```

New session:

```julia
using Parameters
using BSON
@with_kw mutable struct A
  a::Tuple
  b::Bool = false
end
BSON.@load "o.bson" o
@assert o.a == (1, 2)

```

---

<div class="post-metadata">

**Author:** ![cesarmarinhorj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cesarmarinhorj/32/30948_2.png) [@cesarmarinhorj](https://discourse.julialang.org/u/cesarmarinhorj)\
**Post date:** [December 20, 2021, 11:33pm UTC](https://discourse.julialang.org/t/optional-field-in-struct/73338/4 "2021-12-20T23:33:14Z")

</div>

I am still learning, but in this case, a “nullable” type could be:  
struct A  
a::Union{Tuple,Nothing}  
end

a1 = A((1,2,3))  
a2 = A(nothing)

this can be saved in BSON too…

“Nullable” is most common in structs wiith many fields, like

struct Person  
name::String  
age::Union{Int64,Nothing}  
end

in this case, must create two constructors:

Person(name)  
Person(name, age)

age recieves nothing in first case
