Course of Raku / Advanced / Subroutines / Multiple dispatch

Quiz — Dispatch with where

What does the following program print?

multi sub f(Int $n where $n %% 2) { say 'even' }
multi sub f(Int $n)               { say 'odd' }

f(4);
1even
0odd
0both
0nothing

4 satisfies the where $n %% 2 condition, so the more specific candidate is chosen and even is printed. The plain Int candidate is the catch-all for everything that fails the condition.

Course navigation

Solution: Classify the size   |   More on MAIN subroutines