Skip to content

Instantly share code, notes, and snippets.

@moritz
Created February 14, 2012 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save moritz/1826725 to your computer and use it in GitHub Desktop.
Save moritz/1826725 to your computer and use it in GitHub Desktop.
First experiments with
class ObjHash {
has %!keys handles (keys => 'values');
has %!values handles <values elems>;
method at_key(Mu $key) is rw {
my $lkey = $key.WHICH;
%!values.exists($lkey)
?? %!values{$lkey}
!! pir::setattribute__0PPsP(my $v, Scalar, '$!whence',
-> {
say "in whence; v='$v'";
%!keys{$lkey} = $key;
%!values{$lkey} = $v ;
}
);
}
method bind_key(Mu $key, \$value) {
my $lkey = $key.WHICH;
%!keys{$lkey} = $key;
%!values{$lkey} := $value;
}
method pairs() {
%!keys.keys.map({; %!keys{$_} => %!values{$_}});
}
method kv() {
%!keys.keys.map({; %!keys{$_}, %!values{$_}});
}
}
# works:
my $x = ObjHash.new;
my $a = (class A { }).new;
$x{$a} := 'foo';
say $x{$a};
say $x.keys.[0] === $a;
# fails:
$x{'foo'} = 'blub';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment