How to perform a deep copy during an assignment of a value type?

Suppose I have a value type representing some collection that internally uses dynamically allocated class instances to store its data (e.g. in form of a tree). When I assign (copy) an instance of the value type to another variable, I want an independent deep copy of that data to be created (I do not want copy-on-write behavior). How can I do that in Swift? In C++ I would create a copy constructor and overloaded assignment operator for that purpose. Are there corresponding language features or some replacement of them in Swift?

Swift does not currently let you override copying behavior. Copy-on-write is the preferred mechanism for maintaining independent state between copies of a value.

1 Like