如果我将一个列表放在一个数组变量中并将其克隆到另一个数组变量中,则两者是不同的:
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})";
输出显示它们不会相互影响:
original is finally <1 3 Dog> (Array)
clone is finally <1 3 7> (Array)
但是,如果我将数组放入标量变量中,则克隆不会将两者分开。改变一个会改变另一个:
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";
输出显示原始和克隆仍然链接,但奇怪的是它们不是同一个对象或容器:
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
这个工作,克隆被分配给一个数组变量:
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})";
但是当原始数组是一个数组并且克隆被分配给一个标量变量时,它就不起作用了:
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})";
这是Rakudo 2017.01。
答案 0 :(得分:1)
在Rakudo 2017.04中,这不再是一个问题。我得到了预期的输出:
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