September 2017
Beginner
402 pages
9h 52m
English
The ~~ operator is the smartmatch operator. It performs different types of comparison for operands of different types.
my $int = 10;say $int ~~ 10; # Truemy $str = 'str';say $str ~~ 'str'; # Truesay $str ~~ /^ str $/; # ⌈str⌋
As you can see from the output from this test program, the result of the ~~ operator is not always a Boolean value.
Internally, the $a ~~ $b construction with the smartmatch operator is equivalent to the call of $b.ACCEPTS($a). The ACCEPTS method is a built-in method defined for all types in Perl 6. The preceding three smartmatch operations may be rewritten in the following way:
say 10.ACCEPTS($int);say 'str'.ACCEPTS($str);say /^str$/.ACCEPTS($str);
Read now
Unlock full access