Overloaded operators: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Raku}}: Add some verbiage about precedence and symbol reuse) |
|||
Line 570: | Line 570: | ||
While it is very easy to overload operators in Raku, it isn't really common... |
While it is very easy to overload operators in Raku, it isn't really common... |
||
at least, not in the traditional sense. Or it's extremely common... It depends on how you view it. |
at least, not in the traditional sense. Or it's extremely common... It depends on how you view it. |
||
First off, do not confuse "symbol reuse" with operator overloading. Multiplication * and exponentiation ** operators both use an asterisk, but one is not an overload of the other. A single asterisk is not the same as two. In fact the term * (whatever) also exists, but differs from both. The parser is smart enough to know when a term or an operator is expected and will select the correct one (or warn if they are not in the correct position). |
|||
For example: |
|||
<lang perl6>1, 2, 1, * ** * * * … *</lang> |
|||
is a perfectly cromulent sequence definition in Raku. A little odd perhaps, be completely sensible. (It's the sequence starting with givens 1,2,1, with each term after the value of the term 3 terms back, raised to the power of the term two terms back, multiplied by the value one term back, continuing for some undefined number of terms. - Whatever to the whatever times whatever until whatever.) |
|||
One of the founding principles of Raku is that: "Different things should look different". It follows that "Similar things should look similar". |
One of the founding principles of Raku is that: "Different things should look different". It follows that "Similar things should look similar". |
||
Line 622: | Line 632: | ||
3.00.5e1 |
3.00.5e1 |
||
2 3 45 6 # default stringification, then concatenate</pre> |
2 3 45 6 # default stringification, then concatenate</pre> |
||
There is nothing preventing you from overloading or overriding existing |
There is nothing preventing you from overloading or overriding existing |
||
Line 633: | Line 644: | ||
prefix, postfix, (or post-circumfix!) The precedence, associativity and arity |
prefix, postfix, (or post-circumfix!) The precedence, associativity and arity |
||
are all easily defined. An operator at heart is just a subroutine with funny calling conventions. |
are all easily defined. An operator at heart is just a subroutine with funny calling conventions. |
||
Borrowed from the [[Nimber_arithmetic#Raku|Nimber arithmetic]] task: |
Borrowed from the [[Nimber_arithmetic#Raku|Nimber arithmetic]] task: |
||
Line 656: | Line 668: | ||
{{out}} |
{{out}} |
||
<pre>31562</pre> |
<pre>31562</pre> |
||
Base Raku has 27 different operator precedence levels for built-ins. You could theoretically give a new operator a absolute numeric precedence but it would be difficult to predict exactly what the relative precedence would be. Instead, precedence is set by setting a relative precedence; either equivalent to an existing operator, or, by setting it tighter(higher) or looser(lower) precedence than an existing operator. When tighter or looser precedence is specified, a whole new precedence level is created squeezed in between the named level and its immediate successor (predecessor). The task [[Exponentiation_with_infix_operators_in_(or_operating_on)_the_base#Raku|Exponentiation with infix operators in (or operating on) the base]] demonstrates three different operators that nominally do the same thing, but may yield different results due to differing precedence levels. |
|||