Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Clarifies example
Thanks to @raiph in
https://stackoverflow.com/a/54635282/891440. This (hopefully) closes #2620
  • Loading branch information
JJ committed Feb 13, 2019
1 parent 55a9964 commit 7ff36db
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions doc/Type/Mu.pod6
Expand Up @@ -252,9 +252,10 @@ and then manipulate the clone as needed, before returning it.
=begin code
class Bar {
has $.quux;
has @.foo = <a b>;
has %.bar = <a b c d>;
method clone { nextwith :foo(@!foo.clone) :bar(%!bar.clone) }
method clone { nextwith :foo(@!foo.clone), :bar(%!bar.clone), |%_ }
}
my $o1 = Bar.new;
Expand All @@ -264,10 +265,15 @@ with my $o2 = $o1.clone {
}
# Hash and Array attribute modifications in clone do not affect original:
say $o1; # OUTPUT: «Bar.new(foo => ["a", "b"], bar => {:a("b"), :c("d")})␤»
say $o2; # OUTPUT: «Bar.new(foo => ["Z", "Y"], bar => {:X("W"), :Z("Y")})␤»
say $o1;
# OUTPUT: «Bar.new(quux => 42, foo => ["a", "b"], bar => {:a("b"), :c("d")})␤»
say $o2;
# OUTPUT: «Bar.new(quux => 42, foo => ["Z", "Y"], bar => {:X("W"), :Z("Y")})␤»
=end code
The C<|%_> is needed to slurp the rest of the attributes that would have been
copied via shallow copy.
=head2 method new
multi method new(*%attrinit)
Expand Down

0 comments on commit 7ff36db

Please sign in to comment.