Subroutines & signatures

Multi dispatch

Full

Several sub variants under one name; the best-fitting signature is chosen per call.

A multi sub declares one of several candidates sharing a name. At each call, Raku picks the candidate whose signature best fits the arguments — dispatching on their types, counts, and values.

Dispatch by type #

multi describe(Int $n) {
    "int $n";
}
multi describe(Str $s) {
    "str $s";
}
say describe(42);
say describe("hi");
Output
int 42
str hi

The 42 matches the Int candidate, "hi" the Str one — chosen by the arguments' types, with no manual type-checking in the body.

Dispatch by arity #

Candidates can differ purely in how many parameters they take; the call's argument count picks one.

multi greet()   { "hello" }
multi greet($n) { "hi $n" }
say greet();
say greet("Sam");
Output
hello
hi Sam

Dispatch by value and where #

A candidate can match a literal value or a where predicate, which is how a recursive definition names its base case separately from the general one.

multi fac(0)                        { 1 }
multi fac(Int $n where * > 0)       { $n * fac($n - 1) }
say fac(5);
Output
120

The call fac(0) matches the literal-0 candidate; every positive Int matches the where candidate, which recurses down to it.

Notes #