If I put a list in an array variable and clone it into another array variable, the two are distinct:
my @original = 1, 3, 7;
my @clone = @original.clone;
@original[*-1] = 'Dog';
say "original is finally <@original[]> ({@original.^name})";
say "clone is finally <@clone[]> ({@clone.^name})";
The output shows that they don't affect each other:
original is finally <1 3 Dog> (Array)
clone is finally <1 3 7> (Array)
However, if I put an Array in a scalar variable, the clone doesn't keep the two separate. Changing one changes the other:
my $original = [ 1, 3, 7 ];
say "original is <$original[]> ({$original.^name}) with {$original.elems} values";
my $clone = $original.clone;
say "clone is <$clone[]> ({$clone.^name}) with {$clone.elems} values";
if $original eqv $clone {
say "The original and clone have the same values!";
}
if $original === $clone {
say "The original and clone are the same object!";
}
if $original =:= $clone {
say "The original and clone are the same container!";
}
$original[*-1] = 'Dog';
say "original is finally <$original[]> ({$original.^name}) with {$original.elems} values";
say "clone is finally <$clone[]> ({$clone.^name}) with {$clone.elems} values";
The output shows that the original and the clone are still linked, but curiously they aren't the same object or container:
original is <1 3 7> (Array) with 3 values
clone is <1 3 7> (Array) with 3 values
The original and clone have the same values!
original is finally <1 3 Dog> (Array) with 3 values
clone is finally <1 3 Dog> (Array) with 3 values
This one works, where the clone is assigned to an array variable:
my $original = [ 1, 3, 7 ];
my @clone = $original.clone;
$original[*-1] = 'Dog';
say "original is finally <$original[]> ({$original.^name})";
say "clone is finally <@clone[]> ({@clone.^name})";
But when the original is an array and the clone is assigned to a scalar variable, it doesn't work:
my @original = 1, 3, 7;
my $clone = @original.clone;
@original[*-1] = 'Dog';
say "original is finally <@original[]> ({@original.^name})";
say "clone is finally <$clone[]> ({$clone.^name})";
This is Rakudo 2017.01.
In Rakudo 2017.04, this is no longer a problem. I get the expected output:
original is <1 3 7> (Array) with 3 values
clone is <1 3 7> (Array) with 3 values
The original and clone have the same values!
original is finally <1 3 Dog> (Array) with 3 values
clone is finally <1 3 7> (Array) with 3 values
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With