Multi subs
The signature is an important property of a sub. It not only helps to check the types of the arguments, but Perl 6 also uses it to control the number of arguments passed. For example, declare a function for summation that takes three parameters, but call it with only two arguments:
sub add($x, $y, $z) { return $x + $y + $z; } say add(1, 2);
This program does not work. Again, signature
is our friend:
===SORRY!=== Error while compiling add.pl
Calling add(Int, Int) will never work with declared signature ($x, $y, $z)
at add.pl:5
So, we see that when deciding which function to call, Perl 6 takes into account the number of the arguments as well as their types together with the name of the sub. A programmer can benefit from this feature by creating different versions of the function, which share the same name. The distinction between them will be resolved via their signatures.
Let's now put both variants of the add
function together (I will format it differently this time). To inform...