# How to create an instance of a mutable composite type with most fields left empty?

**URL:** <https://discourse.julialang.org/t/how-to-create-an-instance-of-a-mutable-composite-type-with-most-fields-left-empty/56021>\
**Category:** General Usage\
**Created:** [February 25, 2021, 3:14pm UTC](https://discourse.julialang.org/t/how-to-create-an-instance-of-a-mutable-composite-type-with-most-fields-left-empty/56021 "2021-02-25T15:14:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [February 25, 2021, 3:14pm UTC](https://discourse.julialang.org/t/how-to-create-an-instance-of-a-mutable-composite-type-with-most-fields-left-empty/56021/1 "2021-02-25T15:14:07Z")

</div>

Let’s say I have a mutable composite type and want to create an instance of it. However, at first, I only want to initialise one of the fields (lets say in the example bellow Cost = 120.5) and leave all the others empty. In my real use case of portfolio optimisation the composite type has 38 fields, most of them likely to be left empty, so this would be convenient.

```julia
abstract type Furniture end
mutable struct Sofa <: Furniture
    Name::String
    Colour:: Symbol
    Size::Vector{Float64}
    Cost::Float64
end

```

Of course by “brute force” one can always do, for instance

```julia
s = Sofa("",Symbol(""),[],NaN)
s.Cost = 120.5 

```

but I was hoping to be able to do something similar to this instead

```julia
s = Sofa() # errors naturally
s.Cost = 120.5 

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 25, 2021, 3:15pm UTC](https://discourse.julialang.org/t/how-to-create-an-instance-of-a-mutable-composite-type-with-most-fields-left-empty/56021/2 "2021-02-25T15:15:09Z")

</div>

Have a look at [Constructors · The Julia Language](https://docs.julialang.org/en/v1/manual/constructors/#Incomplete-Initialization)

---

<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:** [February 25, 2021, 3:38pm UTC](https://discourse.julialang.org/t/how-to-create-an-instance-of-a-mutable-composite-type-with-most-fields-left-empty/56021/3 "2021-02-25T15:38:18Z")

</div>

You could do this:

```julia
julia> using Parameters

julia> abstract type Furniture end

julia> @with_kw mutable struct Sofa <: Furniture
           Name::Union{String,Nothing} = nothing
           Colour::Union{Symbol,Nothing} = nothing
           Size::Union{Vector{Float64},Nothing} = nothing
           Cost::Union{Float64,Nothing} = nothing
       end
Sofa

julia> s = Sofa()
Sofa
  Name: Nothing nothing
  Colour: Nothing nothing
  Size: Nothing nothing
  Cost: Nothing nothing

julia> s.Cost = 120.5
120.5

julia> s
Sofa
  Name: Nothing nothing
  Colour: Nothing nothing
  Size: Nothing nothing
  Cost: Float64 120.5

```

It doesn’t look very nice, though. One alternative is to initialize the fields with default values of the expected types instead of nothing and then you would not need to use the `Union` types.

Edit: apparently `missing` has [advantages](https://docs.julialang.org/en/v1/manual/missing/) relative to `nothing`.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [February 25, 2021, 6:33pm UTC](https://discourse.julialang.org/t/how-to-create-an-instance-of-a-mutable-composite-type-with-most-fields-left-empty/56021/4 "2021-02-25T18:33:17Z")

</div>

See [LazilyInitializedFields.jl](https://kristofferc.github.io/LazilyInitializedFields.jl/)

You still have to initialize all fields with `uninit`, but the package makes working with partially initialized structs a bit safer.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [February 25, 2021, 10:49pm UTC](https://discourse.julialang.org/t/how-to-create-an-instance-of-a-mutable-composite-type-with-most-fields-left-empty/56021/5 "2021-02-25T22:49:39Z")

</div>

Assuming you could do `s = Sofa()`, what should immediately calling `s.Cost` do? If the answer is error, then you can do this:

```julia
julia> mutable struct Foo
           x
           y
           function Foo(x)
               f = new()
               f.x = x
               f
           end
       end

julia> Foo(1)
Foo(1, #undef)

julia> Foo(1).x
1

julia> Foo(1).y
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getproperty(::Foo, ::Symbol) at ./Base.jl:33
 [2] top-level scope at REPL[2143]:1

```
