Multi dispatch
FullSeveral 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");
int 42
str hiThe 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");
hello
hi SamDispatch 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);
120The call fac(0) matches the literal-0 candidate; every positive Int matches the where candidate, which recurses down to it.
Notes #
- Candidates can differ by arity (number of parameters), parameter types, or even literal values and
whereconstraints — a candidate for0and a general one, say. - When two candidates fit, the narrower (more specific) one wins; a genuine tie is an "ambiguous dispatch" error.
- If no candidate matches, that is a run-time error listing the candidates tried — the usual signal you need one more
multi. - Ordinary
subandmulti subdon't mix under the same name; pick one style per name.