Last active
December 18, 2015 18:59
-
-
Save samuraisam/5829338 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyMu { | |
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); | |
} | |
my $acc_name := $name.substr(2); | |
if $attr.has-accessor && %twiddles.exists($acc_name) { | |
nqp::getattr($cloned, $package, $name) = %twiddles{$acc_name}; | |
} | |
} | |
$cloned | |
} | |
} | |
class MyTest is MyMu { | |
has @.fields; | |
} | |
class F is MyMu { | |
has $!x; | |
method set($y) {$!x := $y}; | |
method get() { $!x } | |
}; | |
my $f = F.new; | |
$f.set([1,2,3]); | |
say $f.get === $f.clone.get; | |
#my $t1 = MyTest.new(:fields<a b>); | |
#my $t2 = $t1.clone(:fields<c d>); | |
#say 'my mu'; | |
#say $t1.fields; | |
#say $t2.fields; | |
# | |
# | |
#class MyTest1 { | |
# has @.fields; | |
#} | |
# | |
#my $t11 = MyTest1.new(:fields<a b>); | |
#my $t21 = $t11.clone(:fields<c d>); | |
# | |
#say 'regular mu'; | |
#say $t11.fields; | |
#say $t21.fields; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment