Creating operators
Operators in Perl 6 are subroutines. In most cases, operator subs are multisubs. Consider, for example, the +
operator. Its semantic is to add two values, which, in turn, can be of different types. You may ask Perl 6 to add two integers, floating points, or complex numbers. Or, the operands may be of different types in the same call, say, when adding a complex number and an integer. The same +
operator also works fine with the types representing dates. To achieve all of this flexibility, Perl 6 uses multi subs.
Let's briefly lurk into the source code of Rakudo and search for a few definitions of the +
operator:
multi sub infix:<+>(Int:D \a, Int:D \b) multi sub infix:<+>(Num:D \a, Num:D \b) multi sub infix:<+>(Complex:D \a, Complex:D \b) multi sub infix:<+>(Complex:D \a, Num(Real) \b) multi sub infix:<+>(Num(Real) \a, Complex:D \b) multi sub infix:<+>(Date:D $d, Int:D $x) multi sub infix:<+>(Int:D $x, Date:D $d)
The Rakudo compiler...