5 ms·
The operator is called "smart-match". When you do: $a ~~ $b You are in fact doing: $b.ACCEPTS($a) So any object can determine how it will interpret
by lizmat 5y ago
The operator is called "smart-match".
When you do:
$a ~~ $b
You are in fact doing:
$b.ACCEPTS($a)
So any object can determine how it will interpret any other object in smart-matching. The core classes or course have all these methods implemented in the expected way. And you can easily do that for your own classes as well:
class Foo {
multi method ACCEPTS(Int:D $count) { $count == 42 }
multi method ACCEPTS("frobnicate" --> True) { }
}
my $foo = Foo.new;
say 42 ~~ $foo; # True
say 666 ~~ $foo; # False
say "what" ~~ $foo; # False
say "frobnicate" ~~ $foo; # True