Constructor to delay array allocation in mutable struct

Hi,

I wish to define a mutable structure containing - among other things - an array

mutable struct MyType
      values :: Matrix{Float} # stands for arbitrary Array of arbitrary data
end

I then wish to create a variable of this type without allocating the Array because I do not yet know what length it will be. To this effect, I should define a constructor

MyType() = MyType(???)

The ??? stands for “how do I tell Julia to wait 'til I’m good and ready to allocate an array”. [] works if my array is a Vector, but not otherwise (and for all I know could be giving work to the GC)

:slightly_smiling_face:

Philippe

I think you need so called incomplete initialization

https://docs.julialang.org/en/v1/manual/constructors/#Incomplete-Initialization

Bingo!

Thank you!

mutable struct MyType
      values :: Matrix{Float} # stands for arbitrary Array of arbitrary data
      MyType() = new()
end
obj = MyType()
1 Like