Hashes that die when reading undefined values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | |
5 | |
10 | |
No such hash key 'x' | |
in sub <anon> at safe-hash.pl:10 | |
in any <anon> at src/gen/BOOTSTRAP.pm:306 | |
in sub say at src/gen/CORE.setting:6226 | |
in block <anon> at safe-hash.pl:24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
role safety { | |
method at_key($key) is rw { | |
my $store = sub ($, $value) { self.STORE_AT_KEY($key, $value) }; | |
self.exists($key) | |
?? Proxy.new( | |
FETCH => sub (|$) { self.EnumMap::at_key($key) }, | |
STORE => $store | |
) | |
!! Proxy.new( | |
FETCH => sub (|$) { die "No such hash key '$key'" }, | |
STORE => $store, | |
); | |
} | |
} | |
my %h; | |
%h does safety; | |
%h = a => 1; | |
say %h<a>; | |
%h<a> = 5; | |
say %h<a>; | |
%h<b> = 10; | |
say %h<b>; | |
say %h<x> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment