Hello,
This question has probably been answered several times, but oddly enough this morning I can’t get the proper answer.
Context: I have a code base with several mutable custom parametric types (i.e. structures which in the application context I call “components”), each having quite many fields (actual code base is Microgrids.jl/src/components.jl at main · Microgrids-X/Microgrids.jl · GitHub):
mutable struct CompA
p1::Float64
#...
pn::Float64
end
# CompB, CompC...
mutable struct CompZ
p1::Float64
#...
pn::Float64
end
Then on one morning I realize I may need an implementation of the Base.copy
method. For one component, I write it manually, but then I realize I may need to do it for all of them…
I found a discussion about iterating over fieldnames
in How to copy all fields without changing the referece?, where a warning is given about a performance impact (and I’m not impacted by performance issue for my potential usage of copy). But oddly enough, I didn’t find a generic recipe to avoid manually typing each field name.
Here is a recipe I ended up with:
function Base.copy(c::CompA)
T = typeof(c)
args = (getfield(c, name) for name in fieldnames(T))
return T(args...)
end
I can duplicate this definition for each component for which I need copy
. Also, I think that this copy
definition is generic enough so that if some types inherit from one common super type, I just need to define it for the supertype, correct?
I’ll be glad to get feedback on the topic, or simply links to relevant previous discussions. I also had in mind there existed a package with macros specifically meant for this use case.