Skip to content

Instantly share code, notes, and snippets.

@lizmat
Last active August 29, 2015 14:01
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 lizmat/7f7ecc7fdbd4152c9932 to your computer and use it in GitHub Desktop.
Save lizmat/7f7ecc7fdbd4152c9932 to your computer and use it in GitHub Desktop.
Can someone explain why FETCH is called *EIGHT* times ???
class A is Hash {
method at_key(A:D $hash: $k) {
Proxy.new(
FETCH => {
say "in FETCH with $k";
$hash.exists_key($k) ?? $hash{$k} !! Int;
},
STORE => -> $old, $new {
if $new > 0 {
$hash{$k} = $new;
$new;
}
elsif $new == 0 {
$hash.delete_key($k);
$new;
}
else { # we don't know about negative values
Int;
}
}
);
}
}
my $a = A.new;
say $a<b>++;
========================================
in FETCH with b
in FETCH with b
in FETCH with b
in FETCH with b
in FETCH with b
in FETCH with b
in FETCH with b
in FETCH with b
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment