Skip to content

Instantly share code, notes, and snippets.

@samuraisam
Last active December 18, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuraisam/5834349 to your computer and use it in GitHub Desktop.
Save samuraisam/5834349 to your computer and use it in GitHub Desktop.
use Test;
use MONKEY_TYPING;
augment class Cool {
method clone(*%twiddles) {
my $cloned := nqp::clone(nqp::decont(self));
for self.^attributes() -> $attr {
my $name := $attr.name;
my $package := $attr.package;
unless nqp::objprimspec($attr.type) {
my $attr_val := nqp::getattr($cloned, $package, $name);
nqp::bindattr($cloned, $package, $name, nqp::clone($attr_val.VAR))
if nqp::iscont($attr_val) || nqp::isconcrete($attr_val.VAR);
}
my $acc_name := $name.substr(2);
if $attr.has-accessor && %twiddles.exists($acc_name) {
nqp::getattr($cloned, $package, $name) = %twiddles{$acc_name};
}
}
$cloned
}
}
my ($p, $q);
$p = 'a' ~~ /$<foo>='a'/;
# previously it was timeout on Rakudo
lives_ok { $q = $p.clone }, 'Match object can be cloned';
is ~$q{'foo'}, 'a', 'cloned Match object retained named capture value';
my class ArrTest is Cool { has @.array; }
my $a1 = ArrTest.new(:array<a b>);
my $a2 = $a1.clone(:array<c d>);
is_deeply $a1.array, ['a', 'b'], 'original object has its original array';
is_deeply $a2.array, ['c', 'd'], 'cloned object has the newly-provided array';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment