Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
show how to change default type for %-sigiled containers
  • Loading branch information
gfldex committed Jun 15, 2016
1 parent de1690d commit 0ba409e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions doc/Language/variables.pod
Expand Up @@ -29,6 +29,27 @@ Examples:
my @array = 1, 2, 3; # Array variable with three elements
my %hash = London => 'UK', Berlin => 'Germany';
The default type can be set with C<is>.
class FailHash is Hash {
has Bool $!final = False;
multi method AT-KEY ( ::?CLASS:D: Str:D \key ){
fail X::OutOfRange.new(:what("Hash key"), :got(key), :range(self.keys)) if $!final && !self.EXISTS-KEY(key);
callsame
}
method finalize () {
$!final = True
}
}
my %h is FailHash = oranges => "round", bananas => "bendy";
say %h<oranges>;
# OUTPUT «round␤»
%h.finalize;
say %h<cherry>;
# OUTPUT «Hash key out of range. Is: cherry, should be in (oranges bananas)␤ in method AT-KEY at failhash.p6 line 4 [...]»
Variables without sigils are also possible, see
L<sigilless variables|#Sigilless variables>.
Expand Down

0 comments on commit 0ba409e

Please sign in to comment.