copy
var copy(var self);
Make a copy of the object self
.
Usage
var x = new(String, $S("Hello"));
var y = copy(x);
show(x); /* Hello */
show(y); /* Hello */
show($I(eq(x, y))); /* 1 */
show($I(x is y)); /* 0 */
Copyable
The Copy
class can be used to override the behaviour of an object when a copy is made of it. By default the Copy
class allocates a new empty object of the same type and uses the Assign
class to set the contents. The copy is then registered with the Garbage Collector as if it had been constructed with new
. This means when using manual memory management a copy must be deleted manually.
If the copy
class is overridden then the implementer may manually have to register the object with the Garbage Collector if they wish for it to be tracked.
By convention copy
follows the semantics of Assign
, which typically means a deep copy should be made, and that an object will create a copy of all of the sub-objects it references or contains - although this could vary depending on the type's overridden behaviours.
struct Copy {
var (*copy)(var);
};