11.2 Operator declarations

To dene the action of an operator is much like dening a function:

_________________________________________________________________________________________________________ Operator denitions
-- --operator de  nition operator--assignment operator de  nition--
                              -|arithmetic operator de nition--|
                              -comparision operator de nition-
- ----result identi  er --: -result type-;- subroutine block------------------
     --------------

-- --assignment operator de nition -:= - (- value parameter-)----------------

-- --arithmetic operator de  nition|+---( -parameter list )----------------
                             |----|
                             | *--|
                             | /--|
                             -** --

-- --comparision operator de nition-|-= --( -parameter list-)---------------
                              |-<  -|
                              | < =-|
                              |->  -|
                              - > =-
___________________________________________________________________

The parameter list for a comparision operator or an arithmetic operator must always contain 2 parameters. The result type of the comparision operator must be Boolean.

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

If the result identier 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 dened; 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 denitions will be examined. As an example, throughout this chapter the following type will be used to dene 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 a unit ucomplex, which contains a complete calculus for complex numbers, based on operator overloading.