Operator Methods - brombres/Rogue GitHub Wiki

Syntax

Operator Method Definitions

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

Definable Binary Operator Methods

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 <>.

Description

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:

  1. A.operator+(A,B) (global method)
  2. A.operator+(B)
  3. 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 ...

Definable Comparison Operator Methods

Operator Name
== Equals
<> Compare

Syntax

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.

Definable Unary Operator Methods

Operator Name
? Logicalize
- Negate
++ Increment
-- Decrement
~ Bitwise Xor
! Bitwise Complement (Bitwise Not)

Notes

  • 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 no operator++ method is defined then Rogue falls back to use a += 1 if method operator+= is defined or else a = a + 1 if only operator+ is defined.

Definable Op-and-Assign Operator Methods

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

Syntax

class XYZ
  GLOBAL METHODS
    method operator+=( left:XYZ, right:SomeType )
       ...

  METHODS
    method operator-=( right:SomeType )
      ...

Notes

  • In some expression a += b, if no operator+= method is defined then Rogue falls back to use a = a + b if method operator+ is defined.
  • .= (Access-and-Assign) cannot be defined as an operator method.
⚠️ **GitHub.com Fallback** ⚠️