Overloaded operators: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(20 intermediate revisions by 10 users not shown)
Line 10:
 
Many commands have multiple [[6502_Assembly#Addressing_Modes| addressing modes]], which alter the way a command is executed. On the 6502 most of these are in fact different opcodes, using the same mnemonic.
<langsyntaxhighlight lang="6502asm">LDA #$80 ;load the value 0x80 (decimal 128) into the accumulator.
LDA $80 ;load the value stored at zero page memory address $80
LDA $2080 ;load the value stored at absolute memory address $2080.
LDA $80,x ;load the value stored at memory address ($80+x).
LDA ($80,x) ;use the values stored at $80+x and $81+x as a 16-bit memory address to load from.
LDA ($80),y ;use the values stored at $80 and $81 as a 16-bit memory address to load from. Load from that address + y.</langsyntaxhighlight>
 
=={{header|68000 Assembly}}==
Most assemblers will interpret instructions such as <code>MOVE</code>, <code>ADD</code>, etc. according to the operand types provided.
<syntaxhighlight lang="68000devpac">MOVE.L D0,A0 ;assembles the same as MOVEA.L D0,A0
EOR.W #%1100000000000000,D5 ;assembles the same as EORI.W #%1100000000000000,D5
CMP.W myAddress,D4 ;assembles the same as CMPM.W myAddress,D4</syntaxhighlight>
 
If you use <code>MOVE.W</code> with an address register as the destination, the CPU will sign-extend the value once it's loaded into the address register. In other words, a word that is $8000 or "more" will get padded to the left with Fs, whereas anything $7FFF or less will be padded with zeroes.
<syntaxhighlight lang="68000devpac">MOVE.W #$9001,A0 ;equivalent of MOVEA.L #$FFFF9001,A0
MOVE.W #$200,A1 ;equivalent of MOVEA.L #$00000200,A1</syntaxhighlight>
 
=={{header|Ada}}==
Many Ada standard libraries overload operators. The following examples are taken from the Ada Language Reference Manual describing operators for vector and matrix types:
 
<syntaxhighlight lang="ada">type Real_Vector is array (Integer range <>) of Real'Base;
type Real_Matrix is array (Integer range <>, Integer range <>) of Real'Base;
-- Real_Vector arithmetic operations
function "+" (Right : Real_Vector) return Real_Vector;
function "-" (Right : Real_Vector) return Real_Vector;
function "abs" (Right : Real_Vector) return Real_Vector;
function "+" (Left, Right : Real_Vector) return Real_Vector;
function "-" (Left, Right : Real_Vector) return Real_Vector;
function "*" (Left, Right : Real_Vector) return Real'Base;
function "abs" (Right : Real_Vector) return Real'Base;
-- Real_Vector scaling operations
function "*" (Left : Real'Base; Right : Real_Vector)
return Real_Vector;
function "*" (Left : Real_Vector; Right : Real'Base)
return Real_Vector;
function "/" (Left : Real_Vector; Right : Real'Base)
return Real_Vector;
-- Real_Matrix arithmetic operations
function "+" (Right : Real_Matrix) return Real_Matrix;
function "-" (Right : Real_Matrix) return Real_Matrix;
function "abs" (Right : Real_Matrix) return Real_Matrix;
function Transpose (X : Real_Matrix) return Real_Matrix;
function "+" (Left, Right : Real_Matrix) return Real_Matrix;
function "-" (Left, Right : Real_Matrix) return Real_Matrix;
function "*" (Left, Right : Real_Matrix) return Real_Matrix;
function "*" (Left, Right : Real_Vector) return Real_Matrix;
function "*" (Left : Real_Vector; Right : Real_Matrix)
return Real_Vector;
function "*" (Left : Real_Matrix; Right : Real_Vector)
return Real_Vector;
-- Real_Matrix scaling operations
function "*" (Left : Real'Base; Right : Real_Matrix)
return Real_Matrix;
function "*" (Left : Real_Matrix; Right : Real'Base)
return Real_Matrix;
function "/" (Left : Real_Matrix; Right : Real'Base)
return Real_Matrix;</syntaxhighlight>
 
The following examples are from the Ada package Ada.Numerics.Generic_Complex_Types
 
<syntaxhighlight lang="ada"> function "+" (Right : Complex) return Complex;
function "-" (Right : Complex) return Complex;
function Conjugate (X : Complex) return Complex;
 
function "+" (Left, Right : Complex) return Complex;
function "-" (Left, Right : Complex) return Complex;
function "*" (Left, Right : Complex) return Complex;
function "/" (Left, Right : Complex) return Complex;
 
function "**" (Left : Complex; Right : Integer) return Complex;
 
function "+" (Right : Imaginary) return Imaginary;
function "-" (Right : Imaginary) return Imaginary;
function Conjugate (X : Imaginary) return Imaginary renames "-";
function "abs" (Right : Imaginary) return Real'Base;
 
function "+" (Left, Right : Imaginary) return Imaginary;
function "-" (Left, Right : Imaginary) return Imaginary;
function "*" (Left, Right : Imaginary) return Real'Base;
function "/" (Left, Right : Imaginary) return Real'Base;
 
function "**" (Left : Imaginary; Right : Integer) return Complex;
 
function "<" (Left, Right : Imaginary) return Boolean;
function "<=" (Left, Right : Imaginary) return Boolean;
function ">" (Left, Right : Imaginary) return Boolean;
function ">=" (Left, Right : Imaginary) return Boolean;
 
function "+" (Left : Complex; Right : Real'Base) return Complex;
function "+" (Left : Real'Base; Right : Complex) return Complex;
function "-" (Left : Complex; Right : Real'Base) return Complex;
function "-" (Left : Real'Base; Right : Complex) return Complex;
function "*" (Left : Complex; Right : Real'Base) return Complex;
function "*" (Left : Real'Base; Right : Complex) return Complex;
function "/" (Left : Complex; Right : Real'Base) return Complex;
function "/" (Left : Real'Base; Right : Complex) return Complex;
 
function "+" (Left : Complex; Right : Imaginary) return Complex;
function "+" (Left : Imaginary; Right : Complex) return Complex;
function "-" (Left : Complex; Right : Imaginary) return Complex;
function "-" (Left : Imaginary; Right : Complex) return Complex;
function "*" (Left : Complex; Right : Imaginary) return Complex;
function "*" (Left : Imaginary; Right : Complex) return Complex;
function "/" (Left : Complex; Right : Imaginary) return Complex;
function "/" (Left : Imaginary; Right : Complex) return Complex;
 
function "+" (Left : Imaginary; Right : Real'Base) return Complex;
function "+" (Left : Real'Base; Right : Imaginary) return Complex;
function "-" (Left : Imaginary; Right : Real'Base) return Complex;
function "-" (Left : Real'Base; Right : Imaginary) return Complex;
function "*" (Left : Imaginary; Right : Real'Base) return Imaginary;
function "*" (Left : Real'Base; Right : Imaginary) return Imaginary;
function "/" (Left : Imaginary; Right : Real'Base) return Imaginary;
function "/" (Left : Real'Base; Right : Imaginary) return Imaginary;</syntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 23 ⟶ 131:
For new dyadic operators, the priority must be specified (though some implementations provide a default).
In both cases this is done with a PRIO declaration.
<langsyntaxhighlight lang="algol68">BEGIN
# Algol 68 allows operator overloading, both of existing operators and new ones #
# Programmer defined operators can be a "bold word" (uppercase word) or a symbol #
Line 53 ⟶ 161:
OP + = ( INT a, BOOL b )INT: IF b THEN a + 1 ELSE a FI;
print( ( TOSTRING a + " ""+"" " + TOSTRING ( a = 10 ) + " = " + TOSTRING ( a + ( a = 10 ) ), newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 60 ⟶ 168:
10 "+" true = 11
</pre>
 
=={{header|BQN}}==
What would generally be called operators in other languages are the basic functions in BQN. Nearly all functions have overloads, and the monadic and dyadic cases are generally inter-related.
 
For example, <code>⌊</code> is Floor in the monadic case:
<syntaxhighlight lang="bqn"> ⌊4.5
4</syntaxhighlight>
But in the dyadic case, it becomes Minimum:
<syntaxhighlight lang="bqn"> 4 ⌊ 3
3</syntaxhighlight>
 
=={{header|C++}}==
Line 65 ⟶ 183:
 
And yes, I know subtracting cuboids can give negative volumes. It's theoretically possible (remember volume or triple integrals ? ).
<langsyntaxhighlight lang="cpp">
//Aamrun, 4th October 2021
 
Line 144 ⟶ 262:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 155 ⟶ 273:
=={{header|F_Sharp|F#}}==
For those who complain that they can't follow my F# examples perhaps if I do the following it will help them.
<langsyntaxhighlight lang="fsharp">
// Overloaded operators. Nigel Galloway: September 16th., 2021
let (+) (n:int) (g:int) = n-g
printfn "%d" (23+7)
</syntaxhighlight>
</lang>
{{out}}
<pre>
16
</pre>
 
=={{header|FreeBASIC}}==
Operators can be overloaded by default, so the <code>Overload</code> keyword is not needed when declaring custom operators. At least one of the operator's parameters must be of a user-defined type (after all, operators with built-in type parameters are already defined).
 
The following example overloads the member operators <code>Cast</code>(Cast) and <code>*=</code>(Multiply And Assign) for objects of a user-defined type.
 
<syntaxhighlight lang="freebasic">Type Rational
As Integer numerator, denominator
Declare Operator Cast () As Double
Declare Operator Cast () As String
Declare Operator *= (Byref rhs As Rational)
End Type
 
Operator Rational.cast () As Double
Return numerator / denominator
End Operator
 
Operator Rational.cast () As String
Return numerator & "/" & denominator
End Operator
 
Operator Rational.*= (Byref rhs As Rational)
numerator *= rhs.numerator
denominator *= rhs.denominator
End Operator
 
Dim As Rational r1 = (2, 3), r2 = (3, 4)
r1 *= r2
Dim As Double d = r1
Print r1, d</syntaxhighlight>
 
 
=={{header|jq}}==
Line 183 ⟶ 333:
 
Note that `+` is symmetric except for its restriction to object x object, as illustrated by:
<langsyntaxhighlight lang="jq">{"a":1} + {"a": 2} #=> {"a": 2}
 
{"a":2} + {"a": 1} #=> {"a": 1}</langsyntaxhighlight>
Most of the other operators that are usually thought of as "arithmetic" are also overloaded, notably:
<pre>
Line 197 ⟶ 347:
 
The comparison operators can also be used on non-JSON entities as well, e.g.
<syntaxhighlight lang="jq">
<lang jq>
0 < infinite #=> true
nan < 0 #=> true
</syntaxhighlight>
</lang>
 
The logical operators (`and`, `or`, `not`) are also defined for all JSON entities, their logic being
Line 210 ⟶ 360:
`length/0` is defined on all JSON entities except `true` and `false`. Note that it is defined
as the absolute value on JSON numbers, and that:
<langsyntaxhighlight lang="jq">nan|length #=> null</langsyntaxhighlight>
 
It is also worth pointing out that a single name/arity function can have multiple definitions within
Line 216 ⟶ 366:
directly accessible. The functionality of the "outer" definition, however, can be accessed indirectly,
as illustrated by the following contrived example:
<langsyntaxhighlight lang="jq">def foo:
def outer_length: length;
def length: outer_length | tostring;
[outer_length, length];
 
"x" | foo #=> [1, "1"]</langsyntaxhighlight>
 
=={{header|Julia}}==
Most operators in Julia's base syntax are in fact just syntactic sugar for function calls. In particular, the symbols:
<langsyntaxhighlight lang="julia">
* / ÷ % & ⋅ ∘ × ∩ ∧ ⊗ ⊘ ⊙ ⊚ ⊛ ⊠ ⊡ ⊓ ∗ ∙ ∤ ⅋ ≀ ⊼ ⋄ ⋆ ⋇ ⋉ ⋊ ⋋ ⋌ ⋏ ⋒ ⟑ ⦸ ⦼ ⦾ ⦿ ⧶ ⧷ ⨇ ⨰ ⨱ ⨲ ⨳ ⨴ ⨵ ⨶ ⨷ ⨸ ⨻
⨼ ⨽ ⩀ ⩃ ⩄ ⩋ ⩍ ⩎ ⩑ ⩓ ⩕ ⩘ ⩚ ⩜ ⩞ ⩟ ⩠ ⫛ ⊍ ▷ ⨝ ⟕ ⟖ ⟗
</syntaxhighlight>
</lang>
are parsed in the same precedence as the multiplication operator function *, and the symbols:
<langsyntaxhighlight lang="julia">
+ - ⊕ ⊖ ⊞ ⊟ ∪ ∨ ⊔ ± ∓ ∔ ∸ ≏ ⊎ ⊻ ⊽ ⋎ ⋓ ⧺ ⧻ ⨈ ⨢ ⨣ ⨤ ⨥ ⨦ ⨧ ⨨ ⨩ ⨪ ⨫ ⨬ ⨭ ⨮ ⨹ ⨺ ⩁ ⩂ ⩅ ⩊ ⩌ ⩏ ⩐ ⩒ ⩔ ⩖ ⩗ ⩛ ⩝ ⩡ ⩢ ⩣
</syntaxhighlight>
</lang>
are parsed as infix operators with the same precedence as +. There are many other operator symbols that can be used as prefix or as infix operators once defined as a function in Julia.
<br /><br />
symbols that can be used as prefix or as infix operators once defined as a function in Julia.
As a language, much of Julia is organized around the concept of multiple dispatch. Because the language dispatches function calls according to the types of the function arguments, even the base arithmetic operators are in fact overloaded operators in Julia.
<br />
For example,<syntaxhighlight lang="julia">2 * 3</syntaxhighlight> is sent to the function *(x::Int, y::Int)::Int, whereas <syntaxhighlight lang="julia">2.0 * 3.0</syntaxhighlight>
As a language, much of Julia is organized around the concept of multiple dispatch. Because
is dispatched to the overloaded function *(x::Float64, y::Float64)::Float64. Similarly, string concatenation in Julia is with * rather than +, so "hello " * "world" is dispatched to the overloaded function *(x::String, y::String)::String, and other types such as matrices also have arithmetic operators overloaded in base Julia:
the language dispatches function calls according to the types of the function arguments,
<syntaxhighlight lang="julia">julia> x = [1 2; 3 4]; y = [50 60; 70 80]; x + y
even the base arthmetic operators are in fact overloaded operators in Julia.
</br />
For example,<lang julia>2 * 3</lang> is sent to the function *(x::Int, y::Int)::Int, whereas <lang julia>2.0 * 3.0</lang>
is dispatched to the overloaded function *(x::Float64, y::Float64)::Float64. Similarly, string
concatenation in Julia is with * rather than +, so "hello " * "world" is dispatched to the
overloaded function *(x::String, y::String)::String, and other types such as matrices also
have arithmetic operators overloaded in base Julia:
<lang julia>julia> x = [1 2; 3 4]; y = [50 60; 70 80]; x + y
2×2 Matrix{Int64}:
51 62
73 84
</syntaxhighlight>
</lang>
Users may define their own overloaded functions in similar ways, whether or not such operators are already used in base Julia. In general, it is considered bad practice, and as "type piracy", to define a user overloaded operator which dispatches on the same types for which base Julia has already defined the same function. Instead, user defined types can be best made to have analogous operators defined for
the new type so as to leverage existing code made for analogous base types. This can allow generic functions to use new types in efficient and constructive ways.
are already used in base Julia. In general, it is considered bad practice, as "operator piracy", to define a user overloaded
 
operator which dispatches on the same types for which base Julia has already defined the same
=== A simple example ===
function. Instead, user defined types can be best made to have analogous operators defined for
The code below is just given as a simplistic example, since in practice the body of such simple one-liner functions would most likely be used without the overloading syntax.
the new type so as to leverage existing code made for analagous base types. This can allow generic
<syntaxhighlight lang="julia">
functions to use new types in efficient and constructive ways.
import Base.-
 
""" overload - operator on vectors to return new vector from which all == subelem element are removed """
-(vec, subelem) where T = [elem for elem in vec if elem != subelem]
 
""" overload - operator on strings to return new string from which all == char c are removed """
-(s::String, c::Char) = String([ch for ch in s if ch != c])
 
@show [2, 3, 4, 3, 1, 7] - 3 # [2, 3, 4, 3, 1, 7] - 3 = [2, 4, 1, 7]
@show "world" - 'o' # "world" - 'o' = "wrld"
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Define a custom vector object and define how plus works on these objects:
<syntaxhighlight lang="mathematica">vec[{a_, b_}] + vec[{c_, d_}] ^:= vec[{a + c, b + d}]
vec[{4, 7}] + vec[{9, 3}]</syntaxhighlight>
{{out}}
<pre>vec[{13, 10}]</pre>
 
=={{header|Nim}}==
Nim allows overloading of operators. There is no restrictions regarding types of arguments when overloading an operator. For instance, we may define a vector type and addition of vectors:
 
<langsyntaxhighlight Nimlang="nim">type Vector = tuple[x, y, z: float]
 
func `+`(a, b: Vector): Vector = (a.x + b.x, a.y + b.y, a.z + b.z)
 
echo (1.0, 2.0, 3.0) + (4.0, 5.0, 6.0) # print (x: 5.0, y: 7.0, z: 9.0)</langsyntaxhighlight>
 
The list of predefined operators with their precedence can be found here: https://rosettacode.org/wiki/Operator_precedence#Nim
Line 276 ⟶ 437:
For instance, we may define an operator <code>^^</code> the following way:
 
<langsyntaxhighlight Nimlang="nim">func `^^`(a, b: int): int = a * a + b * b</langsyntaxhighlight>
 
To determine the precedence of user-defined operators, Nim defines a set of rules:
Line 291 ⟶ 452:
 
Otherwise, precedence is determined by the first character.
 
=={{header|Perl}}==
See 'perldoc overload' for perl's overload capabilities. This example defines a class(package)
that represent non-negative numbers as a string of 1's and overloads the basic math operators
so that they can be used on members of that class(package). Also see 'Zeckendorf arithmetic' where overloading
is used on Zeckendorf numbers.
<syntaxhighlight lang="perl">use v5.36;
 
package Ones;
use overload qw("" asstring + add - subtract * multiply / divide);
 
sub new ( $class, $value ) { bless \('1' x $value), ref $class || $class }
sub asstring ($self, $other, $) { $$self }
sub asdecimal ($self, $other, $) { length $$self }
sub add ($self, $other, $) { bless \($$self . $$other), ref $self }
sub subtract ($self, $other, $) { bless \($$self =~ s/$$other//r), ref $self }
sub multiply ($self, $other, $) { bless \($$self =~ s/1/$$other/gr), ref $self }
sub divide ($self, $other, $) { $self->new( $$self =~ s/$$other/$$other/g ) }
 
package main;
 
my($x,$y,$z) = ( Ones->new(15), Ones->new(4) );
$z = $x + $y; say "$x + $y = $z";
$z = $x - $y; say "$x - $y = $z";
$z = $x * $y; say "$x * $y = $z";
$z = $x / $y; say "$x / $y = $z";</syntaxhighlight>
{{out}}
<pre>
111111111111111 + 1111 = 1111111111111111111
111111111111111 - 1111 = 11111111111
111111111111111 * 1111 = 111111111111111111111111111111111111111111111111111111111111
111111111111111 / 1111 = 111
</pre>
 
=={{header|Phix}}==
Line 312 ⟶ 506:
Inline assembly mnemonics have multiple implicit addressing modes as per the standard intel syntax.
 
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%g\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3.5</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 6.5</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%t\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3.5</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">3</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- true</span>
Line 330 ⟶ 524:
mov eax,1 -- etc
}
<!--</langsyntaxhighlight>-->
 
=={{header|Raku}}==
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.
 
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:
 
<syntaxhighlight lang="raku" line>1, 2, 1, * ** * * * … *</syntaxhighlight>
 
is a perfectly cromulent sequence definition in Raku. A little odd perhaps, but 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".
Line 350 ⟶ 554:
 
Addition:
<syntaxhighlight lang="raku" perl6line>say 3 + 5; # Int plus Int
say 3.0 + 0.5e1; # Rat plus Num
say '3' + 5; # Str plus Int
Line 356 ⟶ 560:
say '3' + '5'; # Str plus Str
say '3.0' + '0.5e1'; # Str plus Str
say (2, 3, 4) + [5, 6]; # List plus Array</langsyntaxhighlight>
 
+ is a numeric operator so every thing is evaluated numerically if possible
Line 370 ⟶ 574:
 
Concatenation:
<syntaxhighlight lang="raku" perl6line>say 3 ~ 5; # Int concatenate Int
say 3.0 ~ 0.5e1; # Rat concatenate Num
say '3' ~ 5; # Str concatenate Int
Line 376 ⟶ 580:
say '3' ~ '5'; # Str concatenate Str
say '3.0' ~ '0.5e1'; # Str concatenate Str
say (2, 3, 4) ~ [5, 6]; # List concatenate Array</langsyntaxhighlight>
 
~ is a Stringy operator so everything is evaluated as a string (numerics are evaluated numerically then coerced to a string).
Line 387 ⟶ 591:
3.00.5e1
2 3 45 6 # default stringification, then concatenate</pre>
 
 
There is nothing preventing you from overloading or overriding existing
Line 398 ⟶ 603:
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.
 
 
Borrowed from the [[Nimber_arithmetic#Raku|Nimber arithmetic]] task:
Line 406 ⟶ 612:
itself.
 
<syntaxhighlight lang="raku" perl6line>sub infix:<⊕> (Int $x, Int $y) is equiv(&infix:<+>) { $x +^ $y }
 
sub infix:<⊗> (Int $x, Int $y) is equiv(&infix:<×>) {
Line 418 ⟶ 624:
}
 
say 123 ⊗ 456;</langsyntaxhighlight>
{{out}}
<pre>31562</pre>
 
 
Base Raku has 27 different operator precedence levels for built-ins. You could theoretically give a new operator an 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.
 
 
Line 429 ⟶ 638:
 
Very, '''very''' basic Line class:
<syntaxhighlight lang="raku" perl6line>class Line {
has @.start;
has @.end;
Line 449 ⟶ 658:
 
# In operation:
say Line.new(:start(-4,7), :end(5,0)) + Line.new(:start(1,1), :end(2,3));</langsyntaxhighlight>
 
{{out}}
Line 467 ⟶ 676:
 
<br>Note that some REXXes may also have other characters (glyphs) for the negation operator &nbsp; (not) &nbsp; such as: &nbsp; &nbsp; <big>^</big> &nbsp; and/or &nbsp; <big>¬</big> &nbsp; glyphs.
<langsyntaxhighlight lang="rexx">/*REXX pgm shows overloading of some operators: prefix/addition/subtraction/concatenate.*/
say '──positive prefix──'
say +5 /* positive prefix integer */
Line 593 ⟶ 802:
say '1' && (0) /* binary XOR'ed binary */
 
exit 0 /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 722 ⟶ 931:
 
However, whilst it is very useful for classes representing mathematical objects, it should otherwise be used sparingly as code can become unreadable if it is used inappropriately.
<langsyntaxhighlight ecmascriptlang="wren">import "./date" for Date
 
var s1 = "Rosetta "
Line 742 ⟶ 951:
var d2 = Date.new(2021, 9, 13)
var i1 = (d2 - d1).days
System.print("i1 = %(i1) days")</langsyntaxhighlight>
 
{{out}}
9,482

edits