Skip to content

Commit c71c154

Browse files
committed
Modify example to show explicit cloning vs binding
Example previously mentions the pitfalls of implicit binding when creating a Pair and suggests cloning. Now shows cloning explicitly.
1 parent 90f7097 commit c71c154

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

doc/Language/variables.pod6

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,13 +630,17 @@ Many operators come with implicit binding which can lead to actions at a distanc
630630
Use C<.clone> or coercion to create a new container that can be bound to.
631631
632632
my @a;
633+
my @a-cloned;
633634
sub f() {
634635
state $i;
635636
$i++;
636-
@a.push: "k$i" => $i # <-- .clone goes here
637+
@a\ .push: "k$i" => $i;
638+
@a-cloned.push: "k$i" => $i.clone;
637639
};
640+
638641
f for 1..3;
639-
dd @a; # OUTPUT: «Array @a = [:k1(3), :k2(3), :k3(3)]␤»
642+
say @a; # OUTPUT: «[k1 => 3 k2 => 3 k3 => 3]␤»
643+
say @a-cloned; # OUTPUT: «[k1 => 1 k2 => 2 k3 => 3]␤»
640644
641645
State variables are shared between all threads. The result can be unexpected.
642646

0 commit comments

Comments
 (0)