Course of Raku / Addendum 🆕 / Flow and functions / Subroutines and functional style / Exercises / Describe by type
Solution: Describe by type
Here is a possible solution to the task.
Code
multi describe(Int $x) { "integer $x" }
multi describe(Str $x) { "string $x" }
multi describe(@x) { "list of {@x.elems}" }
say describe(42);
say describe('hi');
say describe([1, 2, 3]);🦋 You can find the source code in the file multi-describe.raku.
Output
integer 42
string hi
list of 3Comments
Each
multigives one version ofdescribewith a different parameter type. Raku picks the matching candidate by the type of the argument.The
@xsignature matches a list, so the array dispatches to the third candidate, which reports its length.