Rules / Types, classes & roles / exception

X::Cannot::Empty Reference

Error due to inappropriate usage of an empty collection

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method action #

method action()

Verbal description of the inappropriate action.

method what #

method what()

Returns the type that was the target of the action.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

1 no-output · 1 rakupp-differs

class X::Cannot::Empty is Exception { }

Not executed: the documentation states no expected output for this example.

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␤»
Documentation
42
Cannot pop from an empty Stack
Rakudo
42
Cannot pop from an empty Stack
Raku++
42
No such method 'new' for invocant of type 'X::Cannot::Empty'

Raku++ disagrees with both the documentation and Rakudo — a defect.