Overloaded operators: Difference between revisions

Line 16:
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.</lang>
 
 
=={{header|Nim}}==
Nim allows overloading of operators. For instance, we may define a vector type and addition of vectors:
 
<lang 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)</lang>
 
The list of predefined operators with their precedence can be found here: https://rosettacode.org/wiki/Operator_precedence#Nim
 
Nim allows also user defined operators which must be composed using the following characters:
 
= + - * / < >
@ $ ~ & % |
! ? ^ . : \
 
For instance, we may define an operator <code>^^</code> the following way:
 
<lang Nim>func `^^`(a, b: int): int = a * a + b * b</lang>
 
To determine the precedence of user-defined operators, Nim defines a set of rules:
 
Unary operators always bind stronger than any binary operator: <code>$a + b</code> is <code>($a) + b</code> and not <code>$(a + b)</code>.
 
If an unary operator's first character is <code>@</code> it is a sigil-like operator which binds stronger than a primarySuffix: <code>@x.abc</code> is parsed as <code>(@x).abc</code> whereas <code>$x.abc</code> is parsed as <code>$(x.abc)</code>.
 
For binary operators that are not keywords, the precedence is determined by the following rules:
 
Operators ending in either <code>-></code>, <code>~></code> or <code>=></code> are called arrow like, and have the lowest precedence of all operators.
 
If the operator ends with <code>=</code> and its first character is none of <code><</code>, <code>></code>, <code>!</code>, <code>=</code>, <code>~</code>, <code>?</code>, it is an assignment operator which has the second-lowest precedence.
 
Otherwise, precedence is determined by the first character.
 
=={{header|Phix}}==
Anonymous user