Course of Raku / Advanced /
Control flow / given and when
/ Exercises / Match by
type
Solution: Match by type
Here is a possible solution to the task.
Code
my $value = 3.14;
given $value {
when Int { say 'integer' }
when Rat { say 'rational' }
when Str { say 'string' }
}🦋 You can find the source code in the file match-by-type.raku.
Output
rationalComments
When the value to match is a type, the smart match asks whether the topic is of that type.
3.14is not anInt, so the firstwhenis skipped.A decimal literal like
3.14is aRat(a rational number) in Raku, so the secondwhenmatches and the program printsrational.
Course navigation
← Match by
type | gather and
take →