role MutableMethods[&obs] {
method _mutable(::?CLASS $obj, \ret) {
&obs.($obj);
ret
}
}
multi trait_mod:<is>(Method $m, :$mutable!) {
$m.wrap: method (|) {
my \ret = callsame;
do if self ~~ MutableMethods && ret ~~ List && ret.head ~~ self.WHAT {
$._mutable: |ret
} else {
ret
}
}
}
class C {
has Int $.value;
method incr is mutable {
$.clone(:value($!value + 1)),
1
}
}
sub mutate($obj, &block) {
#my @lex = do for @objs <-> $obj {
my $o = $obj but MutableMethods[{
$o = $_
}];
#}
block $o;
$obj.new: |$o.^attributes.map({ .name.substr(2) => .get_value: $o}).Map
}
my $c = C.new: :1value;
say mutate $c, {
.say;
say .incr;
.say;
say .value;
say .incr;
.say;
say .value;
}
say $c;