如果我将一个列表放在一个数组变量中并将其克隆到另一个数组变量中,则两者是不同的:
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,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,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,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.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。