Subroutines & signatures
Return types
FullDeclare 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
IntWith parameters #
sub double($x --> Int) {
$x * 2;
}
say double(21);
Output
42Notes #
-->is a constraint, not a coercion: returning a value of the wrong type is an error, it isn't silently converted. Use an explicit coercion in the body if needed.- The return type also documents intent at the call site — tools and readers see the contract in the signature.
Nil/Failurereturns are always allowed regardless of the declared type, sofailstill works in a typed routine.