5
如果我把一個列表中的數組變量,並將它克隆到另一個數組變量,兩者是有區別的:什麼時候Perl 6數組克隆不是克隆?
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。
這是一個錯誤,並在Rakudo'2017.02'中修復。 在Rakudo 2017.01及之前版本中,'Array'類沒有自己的'clone'方法,所以調用'$ a.clone'實際調用'Mu.clone'。 – cuonglm
這是否有票?我看着RT,並沒有找到一個。 –
它在這裏https://rt.perl.org/Public/Bug/Display.html?id=129762 – cuonglm