Operators

Smartmatch ~~

Full

One 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
True

Against a range #

say 5 ~~ 1..10;
say 20 ~~ 1..10;
Output
True
False

Against 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
False

Notes #