Skip to content

Instantly share code, notes, and snippets.

@tadzik
Created March 1, 2012 17:32
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 tadzik/1951559 to your computer and use it in GitHub Desktop.
Save tadzik/1951559 to your computer and use it in GitHub Desktop.
class Foo does Associative {
has %!elems;
method at_key($k) {
Proxy.new(FETCH => { %!elems.exists($k) ?? %!elems{$k} !! 0 },
STORE => -> $, $val{ %!elems{$k} = $val }
);
}
}
my $a = Foo.new;
$a<foo> = 2;
$a<bar> = 3;
say $a<foo> - 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment