Skip to content

Commit 8ad0c63

Browse files
authored
Document Array.clone
Implemented in Rakudo today in rakudo/rakudo@dc69dafc42
1 parent daa2950 commit 8ad0c63

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/Type/Array.pod6

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,32 @@ Example:
9494
say @a.elems; # OUTPUT: «6␤»
9595
say @a; # OUTPUT: «[a b c d e f]␤»
9696
97+
=head2 method clone
98+
99+
Defined as:
100+
101+
method clone(Array:D: --> Array:D)
102+
103+
Clones the original C<Array>. Modifications of elements in the clone
104+
are not propagated to the original and vice-versa:
105+
106+
my @a = <a b c>; my @b = @a.clone;
107+
@b[1] = 42; @a.push: 72;
108+
say @b; # OUTPUT: «[a 42 c]␤»
109+
say @a; # OUTPUT: «[a b c 72]␤»
110+
111+
However, note that the reifier I<is> shared between the two Arrays,
112+
so both Arrays will have the same elements even when each is randomly-generated
113+
on reification and each element will be reified just once, regardless of
114+
whether the reification was done by the clone or the original Array.
115+
B<Note:> just as reifying an Array from multiple threads is not safe,
116+
so is, for example, reifying the clone from one thread while reifying
117+
the original from another thread is not safe.
118+
119+
my @a = 1, {rand} … ∞; my @b = @a.clone;
120+
say @b[^3]; # OUTPUT: «(1 0.0216426755282736 0.567660896142156)␤»
121+
say @a[^3]; # OUTPUT: «(1 0.0216426755282736 0.567660896142156)␤»
122+
97123
=head2 routine shift
98124
99125
Defined as:

0 commit comments

Comments
 (0)