Subroutines & signatures

Return types

Full

Declare what a routine returns with --> in the signature.

A --> in the signature declares the routine's return type. The returned value is type-checked against it, documenting the contract and catching a wrong-typed return.

Declaring a return type #

sub name(--> Type) states the type with no parameters; it can also follow the parameters.

sub answer(--> Int) {
    42;
}
say answer();
say answer().^name;
Output
42
Int

With parameters #

sub double($x --> Int) {
    $x * 2;
}
say double(21);
Output
42

Notes #