I have a set of mutable structures that are derived from an abstract type :
T1 <: AbstractT T2 <: AbstractT T3 <: AbstractT ... Tn <: AbstractT
I need to make a backup of an array of instances of these structures. I could write a
copy function (shallow copy) for each struct but I would like to avoid it and write a
copy function for the abstract type. The problem is that I don’t know how to create an uninitialized instance of a concrete type. Is there a way to instantiate a structure with undefined fields? so later I can make the assignments.
I was thinking in a copy function like this:
function Base.copy(src::AbstractT) dst = make_an_uninit_istance_of(typeof(src)) # <= is there a way to do this? names = fieldnames(typeof(src)) for name in names val = getfield(src, name) setfield!(dst, name, val) end return dst end
Is it possible? Any ideas?