When and Where
— 焉知非鱼When and Where
When 可以用在主题化($_
)的语句中 #
Raku 里面有个特殊的变量叫 $_
, 即主题化变量, the variable in question.
for ('Swift', 'PHP', 'Python', 'Perl') -> $item {
say $item when $item ~~ /^P/;
}
输出:
PHP
Python
Perl
for (12, 24, 56, 42) {
.say when *>40
}
输出:
56
42
而 where 用于对类型进行约束:
for ('Swift', 'PHP', 'Python', 'Perl', 42) -> $item where $item ~~ Str {
say $item;
}
输出:
Swift
PHP
Python
Perl
Constraint type check failed for parameter '$item'