Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Proxy surprise
our $count = 0;
class MagicVal {
has Int $.constant;
has Int $.varies = 0;
method varies is rw {
$count++;
return-rw Proxy.new(
# note that FETCH and STORE cannot go through the accessors
# of $.varies again, because that would lead to infinite
# recursion. Use the actual attribute here instead
FETCH => method () { $!varies },
STORE => method ($new) { $!varies = $new + 1 },
);
}
}
my $mv = MagicVal.new(:constant(6), :varies(6));
say $mv.varies.VAR.^name; # Proxy
say $mv.varies.^name; # Method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.