Should we add a deep copy method to data classes?

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.

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.

1 Like

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.

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

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

1 Like