I have a type with many fields and I need to define the function copy!(A,B). My question is: is there any way to copy all the fields automatically? Something like deepcopy but without changing the reference?
As of now, I am doing it manually:
function copy!(A::MyType, B::MyType) = begin
A.f1 = B.f1
A.f2 = B.f2
copy!(A.v1, B.v1)
...
end
Looping over fieldnames with getfield and setfield! is not type-stable so it’s slower. I agree that it would be nice to have a generic version of this.
It can probably be made using a generated function.
Yes, I probably should make this a generated function.
I think I coded this in v0.3 before generated functions.
And then I vaguely remember a push limit use of generated functions. Perhaps something to do precompilation?
Are there any drawbacks to generated functions?