Rules / Types, classes & roles / basic

Callable Reference

Invocable code object

What it is #

Position in the hierarchy #

Inherits from
Does
Consumed byCode, ForeignCode

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 CALL-ME #

method CALL-ME(Callable:D $self: |arguments)

This method is required for the ( ) postcircumfix operator and the .( ) postcircumfix operator. It's what makes an object actually call-able and needs to be overloaded to let a given object act like a routine. If the object needs to be stored in an &-sigiled container, it has to implement Callable. Applying the Callable role is not a requirement to make an object callable;

method Capture #

method Capture()

Throws X::Cannot::Capture.

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.

2 no-output · 2 ok

role Callable { ... }

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

my &a = {;}; # Empty block needs a semicolon
my &b = -> {};
my &c = sub () {};
sub foo() {};
my &d = &foo;

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

class A does Callable {
    submethod CALL-ME(|c){ 'called' }
}
my &a = A;
say a(); # OUTPUT: «called␤»
Output
called

Documentation, Rakudo and Raku++ all agree.

class A {
    has @.values;
    submethod CALL-ME(Int $x where 0 <= * < @!values.elems) {
        @!values[$x]
    }
}
my $a = A.new: values => [4,5,6,7];
say $a(2); # OUTPUT: «6␤»
Output
6

Documentation, Rakudo and Raku++ all agree.