Operators
Smartmatch ~~
FullOne operator that asks "does this match that?" against types, ranges, and more.
The smartmatch operator ~~ asks whether the left value matches the right, where "match" depends on what's on the right: a type tests membership, a range tests containment, a regex tests a pattern, a code block tests a predicate. It is the engine behind when, grep, and given.
Against a type #
say 5 ~~ Int; say "x" ~~ Int; say 3.5 ~~ Real;
Output
True
False
TrueAgainst a range #
say 5 ~~ 1..10; say 20 ~~ 1..10;
Output
True
FalseAgainst a predicate #
A Callable on the right is called with the value; a WhateverCode makes a tidy inline test. (Parenthesise it so the * curries only the predicate, not the whole ~~ expression.)
say 5 ~~ (* > 2); say 1 ~~ (* > 2);
Output
True
FalseNotes #
- The result is always a plain
Bool(unlike a bare regex match, which returns aMatch— smartmatch Booleanises it). - What "match" means is defined by the right-hand side's
ACCEPTSmethod, so any type can define how it smartmatches. when Xis$_ ~~ X, andgrep Xkeeps elements where$_ ~~ X— the same operator throughout, which is why ranges, types, and regexes all work in awhen.