Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4070 from dumarchie/master
Document X::Cannot::Empty
- Loading branch information
Showing
3 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
=begin pod :kind("Type") :subkind("class") :category("exception") | ||
=TITLE class X::Cannot::Empty | ||
=SUBTITLE Error due to inappropriate usage of an empty collection | ||
class X::Cannot::Empty is Exception { } | ||
Error, typically wrapped in a L<Failure|/type/Failure>, when inappropriately | ||
using an empty collection. | ||
For example, the following stack implementation fails when trying to pop a value | ||
from an empty stack. Sink context causes the returned C<Failure> to throw. | ||
=begin code | ||
class Stack { | ||
my class Node { | ||
has $.value; | ||
has Node $.next; | ||
} | ||
has Node $!next; | ||
method push($value) { | ||
$!next .= new(:$value, :$!next); | ||
self; | ||
} | ||
method pop() { | ||
fail X::Cannot::Empty.new(:action<pop>, :what(self.^name)) | ||
unless $!next; | ||
my $value = $!next.value; | ||
$!next .= next; | ||
$value; | ||
} | ||
} | ||
my $stack = Stack.new.push(42); | ||
say $stack.pop; # OUTPUT: «42» | ||
try $stack.pop; | ||
say $!.message; # OUTPUT: «Cannot pop from an empty Stack» | ||
=end code | ||
=head1 Methods | ||
=head2 method action | ||
method action() | ||
Verbal description of the inappropriate action. | ||
=head2 method what | ||
method what() | ||
Returns the type that was the target of the action. | ||
=end pod |
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
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