Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #311 Deep copy an object

Create the new object y by cloning the all the contents of x, recursively.

y = Marshal.load(Marshal.dump(x))
let y = structuredClone(x);
classes
var
  x,y: TPersistent;
...
  y.assign(x);
...
use v5.7.3;
use Storable qw(dclone);
my $x = [ 1, 2, [ 'a' ], { x => [3,4] } ];

my $y = dclone $x;
import copy
y = copy.deepcopy(x)

New implementation...
< >
programming-idioms.org