⬆️ ⬇️

Perl6 - Operator Overloading

1. Features of working with variables and literals in Perl6

2. Perl6 - Operations on Variables, Anonymous Blocks

3. Perl6 - Conditional Operators, Loops

4. Perl6 - Working with Functions

5. Perl6 - Classes

6. Perl6 - I / O Modules

7. Perl6 - Comments, whitespace, parentheses

Once I wrote an article about the functions and features of their use (â„–4). Now I want to consider one of their varieties - operators.





We begin, perhaps, with infix operators:

An example of infix operators is the '+' operator.

sub infix:<MyNewOperator> (Int $a, Str $b) { return "Call: $a + $b"; } say 10 MyNewOperator "abc"; 


As a result, we will see on the screen

 Call: 10 + abc 


As you can see, we are not limited in operator names, as it is in c ++ - we can add new ones or change existing ones simply by changing “MyNewOperator” to '+'. As a result, we get the addition operator, which, instead of casting and pasting two strings, will produce a string in the format we specified.



I would also like to add that for the sake of experiment, I tried to add a third argument for the infix operator, and such an operator even compiled successfully, but the only way to call such an operator is no different from calling a normal function:

 sub infix:<MyNewOperator> (Int $a, Str $b, $c) { return "Call: $a + $b, $c"; } say infix:<MyNewOperator>(1, "abc", 5); 


I don’t see any sense in the creation of operators and then I don’t see such use, so I will not deviate further from the “parameter signature”.

')

We proceed to the next type of operators - prefix.

A good example is the negation operator of the logical expression '!' which we will simply overload for integers

 sub prefix:<!> (Int $a) { return "Call: prefix ! $a"; } say !1; 


It is also worth noting that we are again not limited to the names of already existing prefix operators. Thus, I was even able to overload the '.' Operator, which created a very unusual look at the call of such an operator. On this occasion, I even remembered somewhere read comic phrase "In Perl6, you can overload the operators, blocks, everything, and even overload the overload." Joke, but the prefix dot operator somehow makes you think =)



But it's time to move on to other things — postfix operators:

Again, I remembered one article on the subject of small print and one bank:

 sub postfix:<*> (Int $a) { return "See $a paragraph in document A"; } say "We can give you \$1_000_000 just for lulz!"; say 1*; 




As a result, we will see

 We can give you $1_000_000 just for lulz! See 1 paragraph in document A 




Again with regard to all previous types of operators - in fact, we are limited in operator names only to that the compiler must understand where the end of the operator is and where the operand begins. So, you shouldn’t really excel at choosing the operator’s name.

And finally, until we continue, do not forget that the operator is actually the same function, so if you want to make another same postfix operator '*' but for strings, in both use cases you need to add the keyword 'multi'.



But we will move on to the next category - overloading parentheses:

 sub circumfix:<(( ))> (Int $a) { return "<$a>"; } say ((1)); 




As a result, we get

 <1> 




But do not be wise when choosing "new" brackets, because the compiler will have to rake it all out later, but because of your intervention in the list of operators, this may not be so easy =)



However, let's move on to the next type - “postfix brackets”

No matter how I tried to force the compiler to overload this operator for a string or a number, I was given the message that there is no such method for strings. And the examples that I found on the Internet were designed in the form of methods for classes. From this I can assume that it will only happen if we create a new class, so for now we’ll keep using the examples until the following articles, but if you're interested, you can try a compiler on this topic.



Lastly, I also wanted to add that I noticed a working check of the return value type by a function:

 sub Func($a) of Int { return $a; } 


Despite the fact that the function can accept any types of operands, on returning the value, an error will occur at the execution stage, if not an integer number is transmitted, but, for example, a string or a fractional value. As far as I remember the last time I checked, this feature did not work.



On this I would like to finish this article, I hope you enjoyed it.

Source: https://habr.com/ru/post/190186/



All Articles