15.2 Operator declarations

To define the action of an operator is much like defining a function:

_________________________________________________________________________________________________________
Operator definitions

 operator definition operator-----------assignment operator definition----------
                          --------- arithmetic operator definition----------|
                          -logical opecraotmopra driesifionint oiopneratoothre dre ofipnietraiotnor definition
-----result identifier --: -result type- ;- subroutine block------------------
    --------------|

--                       -----  ----- -             -  ----------
  assignment operator definition  -  :=   -| (  value parameter )
                            explicit

--arithmetic operator definition|-+ ---( -parameter list-)---------------
                          ------|
                          --* --|
                          - / --|
                          -*>*< --|

--                        --   --  -           -  ---------------
  comparision operator definition- = -|(  parameter list )
                           - < -|
                           - <>=-|
                           - >=-|
                           - <>-|
                           -in--|

--                    --    --- -           -  -----------------
  logical operator definition-anodr--| (  parameter list  )
                       - xor -|
                       -not -|

--                   --          --- -            - ------------
  other operator definition -enumeinrcator-| (  parameter list )
                      -----dec ----|
___________________________________________________________________

The parameter list for a comparison operator or an arithmetic operator must always contain two parameters, with the exception of the unary minus, where only one parameter is needed. The result type of the comparison operator must be Boolean.

For Delphi compatibility, the symbolic operator names can also be replaced by a plain-text name:


Table 15.1: Operator names

SymbolName


+ add
- subtract
* multiply
/ divide
** power
>< symmetricaldifference
= equal
< lessthan
<= lessthanorequal
> greaterthan
>= greaterthanorequal
<> notequal
:= assign
shr rightshift
shl leftshift
div intdivide
mod modulus
and bitwiseand or logicaland
or bitwiseor or logicalor
xor bitwisexor
not bitwisenot or logicalnot

Remark When compiling in Delphi mode the names of the operators must be used, not the symbols.

Remark When compiling in Delphi mode or Objfpc mode, the result identifier may be dropped. The result can then be accessed through the standard Result symbol.

If the result identifier is dropped and the compiler is not in one of these modes, a syntax error will occur.

The statement block contains the necessary statements to determine the result of the operation. It can contain arbitrary large pieces of code; it is executed whenever the operation is encountered in some expression. The result of the statement block must always be defined; error conditions are not checked by the compiler, and the code must take care of all possible cases, throwing a run-time error if some error condition is encountered.

In the following, the three types of operator definitions will be examined. As an example, throughout this chapter the following type will be used to define overloaded operators on:

type  
  complex = record  
    re : real;  
    im : real;  
  end;

This type will be used in all examples.

The sources of the Run-Time Library contain two units that heavily use operator overloading:

ucomplex
This unit contains a complete calculus for complex numbers.
matrix
This unit contains a complete calculus for matrices.