# Struct with block of missing fields

**URL:** https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869
**Category:** General Usage
**Tags:** struct, parametersjl
**Created:** [June 27, 2023, 2:38am UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869 "2023-06-27T02:38:36Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [June 27, 2023, 2:38am UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869/1 "2023-06-27T02:38:37Z")

</div>

I’d like to have a struct like this.

```julia
@with_kw struct my_struct
    x; y; z

    a
    b=fb(a)
    c=fc(b)
    d=fd(c)
    e=fe(d)

    f=ff(e)
    g=fg(f)
    h=fh(g)
    i=fi(h)
end

```

That can be created in two ways

```julia
   my_struct(;x,y,z, a)         
   my_struct(;x,y,z, f)

```

In the second case, fields a-e would all be `missing`.

Is this possible ? To allow a block of fields to be missing ?

The altenative is to use two structs. It seems nice to have it all in one though, even if half the fields are missing for many instances.

---

<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: [June 27, 2023, 11:29am UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869/2 "2023-06-27T11:29:57Z")

</div>

Why not just write two constructors?

```julia
julia> mutable struct Foo; a; fa; b; fb end

julia> function make_foo(; a)
           Foo(a, 2*a, missing, missing)
       end
make_foo (generic function with 1 method)

julia> function make_foo(; b)
           Foo(missing, missing, b, 2*b)
       end
julia> make_foo(; a= 2)
Foo(2, 4, missing, missing)

```

Though I have to confess that something feels off about this design (several fields computed from other fields and then stored; half of the struct uninitialized). I don’t know the context, but perhaps it’s worth reconsidering the overall design.

Perhaps also useful: [LazyInitializedFields.jl](https://kristofferc.github.io/LazilyInitializedFields.jl/)

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [June 27, 2023, 11:47am UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869/3 "2023-06-27T11:47:28Z")

</div>

Thanks for the library. I’ll check it out.  
you may be right re overall design, there’s just something appealing about a single structure.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 27, 2023, 12:29pm UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869/4 "2023-06-27T12:29:37Z")

</div>

Your code is running in which version? I get:

```julia
julia> function make_foo(; a)
           Foo(a, 2*a, missing, missing)
       end
make_foo (generic function with 1 method)

julia> function make_foo(; b)
           Foo(missing, missing, b, 2*b)
       end
make_foo (generic function with 1 method)

julia> make_foo(; a= 2)
ERROR: UndefKeywordError: keyword argument b not assigned

```

I do not understand how your example could work as methods that only change the keyword arguments do not create new methods, but replace the old ones.

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [June 27, 2023, 12:31pm UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869/5 "2023-06-27T12:31:04Z")

</div>

That wouldn’t work properly as Julia does not dispatch on keyword arguments (see discussion [How can kwargs be used in multiple dispatch - #2 by lmiq](https://discourse.julialang.org/t/how-can-kwargs-be-used-in-multiple-dispatch/86790/2)).

In this particular case, it is not too cumbersome to check all variants of `missing`ness of two kwargs, so I personally would go with that.

On the other hand, I agree that, unless `my_struct` is mutable, a partially-filled case may just be another type. It is even possible to overload `getproperty` so that `.a`-`.e` access would give `missing`, although I’d prefer accessor functions for that.

```julia
function Base.getproperty(x::my_partial_struct, p::Symbol)
    if p in (:a, :b, :c, :d, :e)
        return missing
    else
        return getfield(x, p)
    end
end

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 27, 2023, 12:33pm UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869/6 "2023-06-27T12:33:38Z")

</div>

I’m not 100% sure on this but it seems to me that your original request taken at face value, i.e. having a struct which has both a version where `my_struct_instance.a` exists and one where it doesn’t defeats the purpose of (at least concretely typed) structs, which should signal to the compiler what the memory layout of an object is and therefore allow it to generate optimal code.

It seems to me that if the compiler, when seeing `x::MyStruct`, can’t even reason about what fields `x` does or doesn’t have you’ve basically given up on all the performance benefits of Julia.

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [June 27, 2023, 12:40pm UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869/7 "2023-06-27T12:40:45Z")

</div>

Thanks. I guess multiple structs is the answer here

---

<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: [June 27, 2023, 1:02pm UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869/8 "2023-06-27T13:02:47Z")

</div>

I missed that dispatch issue (only checked one of the constructors) and should have proposed a single constructor along the lines of

> [@Vasily\_Pisarev](#):
>
> n this particular case, it is not too cumbersome to check all variants of `missing`ness of two kwargs, so I personally would go with that.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 27, 2023, 5:54pm UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869/9 "2023-06-27T17:54:00Z")

</div>

I do not think you need to throw away **all** the performance benefits, if these fields can be either `missing` or a value of a specific type (like `Int`) then we can just have all of them as `Union{Missing,Int}` and initialize accordingly in a complex constructor. Then you can use `Base.coalesce` to facilitate access with a default value (and the other many `Base` functions made to interact with `missing`). Of course, `nothing` can be used instead too, if the purpose is to fail soon instead of later.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 27, 2023, 10:17pm UTC](https://discourse.julialang.org/t/struct-with-block-of-missing-fields/100869/10 "2023-06-27T22:17:53Z")

</div>

Sure, but taking it at face value I literally meant the fields don’t exist rather than are initialized as some instance of another type that signifies they don’t exist.
