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.
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
s = Sofa("",Symbol(""),[],NaN)
s.Cost = 120.5
but I was hoping to be able to do something similar to this instead
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 relative to nothing.
Assuming you could do s = Sofa(), what should immediately calling s.Cost do? If the answer is error, then you can do this:
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