Should we add a deep copy method to data classes?


#1

I understand the reasoning for having the default copy() method on a data class be a shallow copy.

But it would be really stellar to have it also generate a deepCopy() method.

There would have to be some restrictions, such as all children must either be primitives, or data classes them selves (or at least implement cloneable), since every member, and every member of every member, would have to also have a deep copy method.

But man, when you need a deep copy, you need a deep copy, and implementing it your self is always terrible and error prone.


#2

If all the nested objects are immutable, what they should be, then a shallow copy is sufficient. So I think that a deep copy method in data classes would be a cure for a problem that should be addressed directly.


#3

What exactly is “deep copy” is depends on specific application. For example, sometimes it’s necessary to prevent recursion. So it would better not be default.


#4

Ah this is a good point. Didn’t think about that.


#5

I retract this idea, these are all good points :stuck_out_tongue: