Operator Methods - brombres/Rogue GitHub Wiki
class XYZ
GLOBAL METHODS
method operator+( left:XYZ, right:SomeType )->ResultType
if (null) return ...
return left.operator+( right )
method operator+( left:SomeType, right:XYZ )->ResultType
...
method operator?( operand:XYZ )->Logical
if (operand is null) return false
return operand.operator?
METHODS
method operator+( right:SomeType )->ResultType
return ...
method operator-( right:SomeType )->ResultType
# Binary minus
...
method operator-()->ResultType
# Unary negate
...
method operator?->Logical
return ...
method operator--
# Unary decrement
...
endClass
| Operator | Name |
|---|---|
| + | Plus |
| - | Minus |
| * | Times |
| / | Divide |
| % | Mod |
| ^ | Power |
| & | Bitwise And |
| | | Bitwise Or |
| :<<: | Bit Shift Left |
| :>>: | Bit Shift Right |
| :>>>: | Bit Shift Right with Sign Extend |
See the next section for comparison operator methods == and <>.
When a binary operator is applied to two operands such as a + b, where a is type A and b is type B, the compiler chooses operator methods in the following order of preference:
-
A.operator+(A,B)(global method) A.operator+(B)-
B.operator+(A,B)(global method)
The advantage of global operator methods is that they can work with null references on the left-hand side.
The advantage of object (non-global) operator methods is that dynamic dispatch ensures that the operator method called depends on the type of the left-hand object rather than the type of the left-hand reference.
A developer might choose to use one or the other - or both in tandem as shown here:
class XYZ
GLOBAL METHODS
method operator+( left:XYZ, right:SomeType )->ResultType
if (left is null) return ...
return left.operator+( right )
METHODS
method operator+( right:SomeType )->ResultType
return ...
| Operator | Name |
|---|---|
| == | Equals |
| <> | Compare |
GLOBAL METHODS
method operator<>( left:ThisType, right:OtherType )->Int32
return ... # negative if left < right, zero if left == right,
# positive if left > right
method operator==( left:OtherType, right:ThisType )->Logical
if (...) return true
else return false
METHODS
method operator<>( right:OtherType )->Int32
...
method operator==( right:OtherType )->Logical
...
Rogue has seven value comparison operators: == != < <= > >= <>.
All seven operators are supported by implementing a single method: method operator<>(...)->Int32. It should return a negative value if "this value"/left-hand value is less than the right-hand value, zero if equal, or positive if greater-than. The compiler generates code to interpret the results for a < operation, >=, etc.
If a class is not "comparable" in terms of greater-than and less-than, the == and != operators can be implemented with method operator==(...)->Logical. Again the compiler takes care of adapting the result from operator==() for the != operator.
It is never necessary for both methods to exist in a class, but if they do then method operator<>() will be used.
| Operator | Name |
|---|---|
| ? | Logicalize |
| - | Negate |
| ++ | Increment |
| -- | Decrement |
| ~ | Bitwise Xor |
| ! | Bitwise Complement (Bitwise Not) |
- Objects and compounds only support pre-increment and pre-decrement (
++obj,--obj) steppers; they do not support post-increment and post-decrement. Only primitives support all pre/post-increment/decrement operations. - In some expression
++a, if nooperator++method is defined then Rogue falls back to usea += 1if methodoperator+=is defined or elsea = a + 1if onlyoperator+is defined.
| Operator | Name |
|---|---|
| += | Add-and-Assign |
| -= | Subtract-and-Assign |
| *= | Multiply-and-Assign |
| /= | Divide-and-Assign |
| %= | Mod-and-Assign |
| ^= | Power-and-Assign |
| &= | Bitwise-And-and-Assign |
| = | Bitwise-Or-and-Assign |
| ~= | Bitwise-Xor-and-Assign |
| :<<: | Bit-Shift-Left-and-Assign |
| :>>: | Bit-Shift-Right-and-Assign |
| :>>>: | Bit-Shift-Right-With-Sign-Extend-and-Assign |
class XYZ
GLOBAL METHODS
method operator+=( left:XYZ, right:SomeType )
...
METHODS
method operator-=( right:SomeType )
...
- In some expression
a += b, if nooperator+=method is defined then Rogue falls back to usea = a + bif methodoperator+is defined. -
.=(Access-and-Assign) cannot be defined as an operator method.