Correct typing of struct field with late initialization

Probably not much, since with nothing or not there you will need in your code to check the value to see if it “empty” or not (meaning, you probably cannot avoid the associated branches anyway).

Probably (of course that depends on what you do with the data). But be aware that @set! does not really mutate the value, it is only a syntax sugar for creating a new instance of the immutable object. That is:

julia> struct A
           x::Int
       end

julia> using Setfield

julia> f(a::A) = @set! a.x = 2
f (generic function with 1 method)

julia> a = A(1)
A(1)

julia> f(a)
A(2)

julia> a
A(1)

julia> a = f(a) # you need to adapt the code to update the variable like this

julia> a
A(2)

2 Likes