class ForeignCode

Rakudo-specific class that wraps around code in other languages (generally NQP)

class ForeignCode does Callable {}

[1]

ForeignCode is a Raku wrapper around code that is not written originally in that language; its intention is to use these blocks of code in Callable contexts easily. For instance, subs have some anonymous functions that are actually ForeignCode.

sub does-nothing(){};
say $_.name ~ ' → ' ~ $_.^name for &does-nothing.^methods;
# OUTPUT: «<anon> → ForeignCode␤<anon> → ForeignCode␤soft → Method␤…» 

This script will map method names to their class, and it shows that routines, in particular, have several methods that are actually ForeignCode instead of Methods.

Methods

method arity

method arity()

Returns the arity of the enclosed code.

method count

method count()

Returns the number of arguments the enclosed code needs.

method signature

method signatureForeignCode:D: )

Returns the signature of the enclosed code.

method name

method name()

Returns the name of the enclosed code, or <anon> if it has not received any.

method gist

method gistForeignCode:D: )

Returns the name of the code by calling name.

method Str

method StrForeignCode:D: )

Returns the name of the code by calling name.

Type Graph

Type relations for ForeignCode
perl6-type-graph ForeignCode ForeignCode Any Any ForeignCode->Any Callable Callable ForeignCode->Callable Mu Mu Any->Mu

Expand above chart

Routines supplied by role Callable

ForeignCode does role Callable, which provides the following routines:

(Callable) 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␤»

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

(Callable) method Capture

Defined as:

method Capture()

Throws X::Cannot::Capture.