role Callable

Invocable code object
role Callable { ... }

Role for objects which support calling them. It's used in Block, Routine, Sub, Method, Submethod and Macro types.

Callables can be stored in &-sigiled containers, the default type constraint of such a container is Callable.

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

Methods

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.

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

Applying the Callable role is not a requirement to make an object callable; if a class simply wants to add subroutine-like semantics in a regular scalar container, the submethod CALL-ME can be used for that.

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␤»

method Capture

method Capture()

Throws X::Cannot::Capture.

See Also

role Dateish

Object that can be treated as a date

role Distribution

Distribution

role Encoding

Support for character encodings.

role PredictiveIterator

Iterators that can predict number of values

class RakuAST::Doc::DeclaratorTarget

Provide leading/trailing doc functionality

role Rational

Number stored as numerator and denominator

role Real

Non-complex number

role Stringy

String or object that can act as a string

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.