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. A signature object can be used to force a check against the signature of the Callable to be stored into the container.

my &a = {;}# Empty block needs a semicolon 
my &b = -> {};
my &c = sub () {};
sub foo() {};
my &d = &foo;
my &f:(Int= sub bar(Int{}# Not yet implemented 
my &f:(Str= -> Str {};       # Not yet implemented 

Methods

method CALL-ME

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

This method is required for postfix:«( )» and postfix:«.( )». 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 a &-sigiled container, is has to implement Callable.

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

method Capture

Defined as:

method Capture()

Throws X::Cannot::Capture.

method assuming

method assuming(Callable:D $self: |primers)

Returns a Callable that implements the same behaviour as the original, but has the values passed to .assuming already bound to the corresponding parameters.

my sub slow($n){ my $i = 0$i++ while $i < $n$i };
 
# takes only one parameter and as such wont forward $n 
sub bench(&c){ cnow - ENTER now };
 
say &slow.assuming(10000000).&bench# OUTPUT: «(10000000 7.5508834)␤» 

For a sub with arity greater than one, you can use Whatever * for all of the positional parameters that are not "assumed".

sub first-and-last ( $first$last ) {
    say "Name is $first $last";
}
 
my &surname-smith = &first-and-last.assuming*'Smith' );
 
&surname-smith.'Joe' ); # OUTPUT: «Name is Joe Smith␤» 

You can handle any combination of assumed and not assumed positional parameters:

sub longer-names ( $first$middle$last$suffix ) {
    say "Name is $first $middle $last $suffix";
}
 
my &surname-public = &longer-names.assuming**'Public'* );
 
&surname-public.'Joe''Q.''Jr.'); # OUTPUT: «Name is Joe Q. Public Jr.␤» 

Named parameters can be assumed as well:

sub foo { say "$^a $^b $:foo $:bar" }
&foo.assuming(13:42foo)(24:72bar); # OUTPUT: «13 24 42 72␤» 

And you can use .assuming on all types of Callables, including Methods and Blocks:

# We use a Whatever star for the invocant: 
my &comber = Str.^lookup('comb').assuming: *, /\w+/;
say comber 'Perl is awesome! Python is great! And PHP is OK too';
# OUTPUT: «(Perl Python PHP)␤» 
 
my &learner = {
    "It took me $:months months to learn $^lang"
}.assuming: 'Perl 6';
say learner :6months;  # OUTPUT: «It took me 6 months to learn Perl 6␤» 

Type Graph

Type relations for Callable
perl6-type-graph Callable Callable Mu Mu Any Any Any->Mu Code Code Code->Callable Code->Any ForeignCode ForeignCode ForeignCode->Callable ForeignCode->Any WhateverCode WhateverCode WhateverCode->Code Block Block Block->Code Routine Routine Routine->Block Macro Macro Macro->Routine Sub Sub Sub->Routine Method Method Method->Routine Submethod Submethod Submethod->Routine Regex Regex Regex->Method

Stand-alone image: vector