Overloaded operators: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Raku}}: more clarification) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 214: | Line 214: | ||
To be fair, all of this easy power in a bad programmers hands can lead to incomprehensible code... but bad programmers can be bad in any language. |
To be fair, all of this easy power in a bad programmers hands can lead to incomprehensible code... but bad programmers can be bad in any language. |
||
=={{header|REXX}}== |
|||
A lot of the examples were taken from the Raku examples. |
|||
The REXX language has the "normal" (as say, compared with PL/I) overloading of: |
|||
* the infix operators (<big>'''+'''</big> and <big>'''-'''</big>) which are shared with the addition and subtraction operators, |
|||
* the multiplication operator (<big>'''*'''</big>) is "shared" with the exponentiation operator (<big>'''**'''</big>), |
|||
* the "or" operator (<big>'''|'''</big>) is "shared" with the concatenation operator (<big>'''||'''</big>), |
|||
* the "and" operator (<big>'''&'''</big>) is "shared" with the "XOR" (eXclusive OR) operator (<big>'''&&'''</big>), and |
|||
* the "negation" operator (<big>'''\'''</big>) is "shared" with the "not" logical comparison operator, as in: <big>''if a\=b then ... </big>'' |
|||
<br>Note that some REXXes may also have other characters (glyphs) for the negation operator (not) such as: <big>^</big> and/or <big>¬</big> glyphs. |
|||
<lang rexx>/*REXX pgm shows "overloading" of some operators: infix/addition/subtraction/concatenate */ |
|||
say '──positive infix──' |
|||
say +5 /* positive infix integer */ |
|||
say + 5 /* positive infix integer */ |
|||
say ++6 /* positive infix integer */ |
|||
say ++ 6 /* positive infix integer */ |
|||
say +++7 /* positive infix integer */ |
|||
say +++ 7 /* positive infix integer */ |
|||
say + + + + 8 /* positive infix integer */ |
|||
say + (9) /* positive infix integer */ |
|||
say '──negative infix──' |
|||
say -1 /* negative infix integer */ |
|||
say - 1 /* negative infix integer */ |
|||
say --2 /* negative infix integer */ |
|||
say -- 2 /* negative infix integer */ |
|||
say ---3 /* negative infix integer */ |
|||
say --- 3 /* negative infix integer */ |
|||
say - - - - 4 /* negative infix integer */ |
|||
say - (9) /* negative infix integer */ |
|||
say '───addition───' |
|||
say 3 + 5 /* integer plus integer */ |
|||
say 3 + (5) /* integer plus integer */ |
|||
say 3.0 + 0.5e1 /* rational plus number */ |
|||
say '3' + 5 /* string plus integer */ |
|||
say 3 + ' 5 ' /* integer plus string */ |
|||
say 3 + '5' /* integer plus string */ |
|||
say '3' + '5' /* string plus string */ |
|||
say '3' + "5" /* string plus string */ |
|||
say '3.0' + '0.5e1' /* string plus string */ |
|||
say '──subtraction──' |
|||
say 3 - 5 /* integer minus integer */ |
|||
say 3 - (5) /* integer minus integer */ |
|||
say 3.0 - 0.5e1 /* rational minus number */ |
|||
say '3' - 5 /* string minus integer */ |
|||
say 3 - '5' /* integer minus string */ |
|||
say 3 - ' 5 ' /* integer minus string */ |
|||
say '3' - '5' /* string minus string */ |
|||
say '3' - "5" /* string minus string */ |
|||
say '3.0' - '0.5e1' /* string minus string */ |
|||
say '──concatenation──' |
|||
say 3 || 5 /* integer concatenated integer */ |
|||
say 3 || (5) /* integer concatenated integer */ |
|||
say 3.0 || 0.5e1 /* rational concatenated number */ |
|||
say '3' || 5 /* string concatenated integer */ |
|||
say 3 || '5' /* integer concatenated string */ |
|||
say '3' || '5' /* string concatenated string */ |
|||
say "3" || "5" /* string concatenated string */ |
|||
say '3.0' | | '0.5e1' /* string concatenated string */ |
|||
say 3 || ' 5 '. /* integer concatenated strings */ |
|||
say '────abutment────' |
|||
say 3 5 /* integer abutted integer */ |
|||
say 3 (5) /* integer abutted integer */ |
|||
say 3.0 0.5e1 /* rational abutted number */ |
|||
say '3' 5 /* string abutted integer */ |
|||
say 3 '5' /* integer abutted string */ |
|||
say '3' '5' /* string abutted string */ |
|||
say "3" "5" /* string abutted string */ |
|||
say 3 ' 5 '. /* integer abutted strings */ |
|||
say '──multiplication──' |
|||
say 3 * 5 /* integer multiplied integer */ |
|||
say 3 * (5) /* integer multiplied integer */ |
|||
say 3.0 * 0.5e1 /* rational multiplied number */ |
|||
say '3' * 5 /* string multiplied integer */ |
|||
say 3 * '5' /* integer multiplied string */ |
|||
say '3' * '5' /* string multiplied string */ |
|||
say "3" * "5" /* string multiplied string */ |
|||
say '3.0' * '0.5e1' /* string multiplied string */ |
|||
say '──exponentation──' |
|||
say 3 ** 5 /* integer exponetiated integer */ |
|||
say 3 ** (5) /* integer exponetiated integer */ |
|||
say 3 * * 5 /* integer exponetiated integer */ |
|||
say 3.0 ** 0.5e1 /* rational exponetiated number */ |
|||
say '3' ** 5 /* string exponetiated integer */ |
|||
say 3 ** '5' /* integer exponetiated string */ |
|||
say '3' ** '5' /* string exponetiated string */ |
|||
say "3" ** "5" /* string exponetiated string */ |
|||
say '3.0' ** '0.5e1' /* string exponetiated string */ |
|||
say '────division────' |
|||
say 3 / 5 /* integer divided integer */ |
|||
say 3 / (5) /* integer divided integer */ |
|||
say 3.0 / 0.5e1 /* rational divided number */ |
|||
say '3' / 5 /* string divided integer */ |
|||
say 3 / '5' /* integer divided string */ |
|||
say '3' / '5' /* string divided string */ |
|||
say "3" / "5" /* string divided string */ |
|||
say '3.0' / '0.5e1' /* string divided string */ |
|||
say '─────not────' |
|||
say \0 /* (not) invert binary */ |
|||
say \1 /* (not) invert binary */ |
|||
say \ 1 /* (not) invert binary */ |
|||
say \ (0) /* (not) invert binary */ |
|||
say \ 1 /* (not) invert binary */ |
|||
say \ (0) /* (not) invert binary */ |
|||
say \\ 0 /* (not) (not) invert binary */ |
|||
say \ \ 1 /* (not) (not) invert binary */ |
|||
say '─────or─────' |
|||
say 0 | 0 /* binary OR'ed binary */ |
|||
say 0 | 1 /* binary OR'ed binary */ |
|||
say '0' | "1" /* binary OR'ed binary */ |
|||
say '1' | 0 /* binary OR'ed binary */ |
|||
say '1' | (0) /* binary OR'ed binary */ |
|||
say '─────and────' |
|||
say 0 & 0 /* binary AND'ed binary */ |
|||
say 0 & 1 /* binary AND'ed binary */ |
|||
say '0' & "1" /* binary AND'ed binary */ |
|||
say '1' & 0 /* binary AND'ed binary */ |
|||
say '1' & (0) /* binary AND'ed binary */ |
|||
say '─────XOR────' |
|||
say 0 && 0 /* binary XOR'ed binary */ |
|||
say 0 && 1 /* binary XOR'ed binary */ |
|||
say '0' && "1" /* binary XOR'ed binary */ |
|||
say '1' && 0 /* binary XOR'ed binary */ |
|||
say '1' && (0) /* binary XOR'ed binary */ |
|||
exit 0 /*stick a fork in it, we're all done. */</lang> |
|||
{{out|output|text= when using the internal default input:}} |
|||
<pre> |
|||
──positive infix── |
|||
5 |
|||
5 |
|||
6 |
|||
6 |
|||
7 |
|||
7 |
|||
8 |
|||
9 |
|||
──negative infix── |
|||
-1 |
|||
-1 |
|||
2 |
|||
2 |
|||
-3 |
|||
-3 |
|||
4 |
|||
-9 |
|||
───addition─── |
|||
8 |
|||
8 |
|||
8.0 |
|||
8 |
|||
8 |
|||
8 |
|||
8 |
|||
8 |
|||
8.0 |
|||
──subtraction── |
|||
-2 |
|||
-2 |
|||
-2.0 |
|||
-2 |
|||
-2 |
|||
-2 |
|||
-2 |
|||
-2 |
|||
-2.0 |
|||
──concatenation── |
|||
35 |
|||
35 |
|||
3.00.5E1 |
|||
35 |
|||
35 |
|||
35 |
|||
35 |
|||
3.00.5e1 |
|||
3 5 . |
|||
────abutment──── |
|||
3 5 |
|||
3 5 |
|||
3.0 0.5E1 |
|||
3 5 |
|||
3 5 |
|||
3 5 |
|||
3 5 |
|||
3 5 . |
|||
──multiplication── |
|||
15 |
|||
15 |
|||
15.0 |
|||
15 |
|||
15 |
|||
15 |
|||
15 |
|||
15.0 |
|||
──exponentation── |
|||
243 |
|||
243 |
|||
243 |
|||
243 |
|||
243 |
|||
243 |
|||
243 |
|||
243 |
|||
243 |
|||
────division──── |
|||
0.6 |
|||
0.6 |
|||
0.6 |
|||
0.6 |
|||
0.6 |
|||
0.6 |
|||
0.6 |
|||
0.6 |
|||
─────not──── |
|||
1 |
|||
0 |
|||
0 |
|||
1 |
|||
0 |
|||
1 |
|||
0 |
|||
1 |
|||
─────or───── |
|||
0 |
|||
1 |
|||
1 |
|||
1 |
|||
1 |
|||
─────and──── |
|||
0 |
|||
0 |
|||
0 |
|||
0 |
|||
0 |
|||
─────XOR──── |
|||
0 |
|||
1 |
|||
1 |
|||
1 |
|||
1 |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |