📘 Sub overloading with subtypes in Perl 6

Multi subs can be made even more specific by using subtypes. In Perl 6, subtypes are created with the subset keyword. A subtype definition takes one of the existing types and adds a restriction to select the values to be included in the subtype range.

The following lines give a clear view of how subtypes are defined. From the same integer type, Int, the Odd subtype selects only the odd numbers, while the Even subtype picks only the even numbers.

subset Odd of Int where {$^n % 2 == 1};
subset Even of Int where {$^n % 2 == 0};

Now, the subtypes can be used in the signatures of the multi subs. The testnum function has two versions, one for odd and one for even numbers.

multi sub testnum(Odd $x) {
    say "$x is odd";

multi sub testnum(Even $x) {
    say "$x is even";

Which function will be used in a call, testnum($x), depends on the actual value of the variable $x. Here is an example with the loop, calling either testnum(Even) for even numbers or testnum(Odd) for odd numbers.

for 1..4 -> $x {

The loop prints a sequence of alternating function call results, which tells us that Perl 6 made a correct choice by using the rules provided in the subtype definitions.

1 is odd 
2 is even 
3 is odd 
4 is even

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s