[prev] [prev-tail] [tail] [up]

### 9.5 Comparision operator

The comparision operator can be overloaded to compare two different types or to compare two equal types that are not basic types. The result type of a comparision operator is always a boolean.

The comparision operators that can be overloaded are:

equal to
  (=) to determine if two variables are equal.
less than
  (<) to determine if one variable is less than another.
greater than
  (>) to determine if one variable is greater than another.
greater than or equal to
  (>=) to determine if one variable is greater than or equal to another.
less than or equal to
  (<=) to determine if one variable is greater than or equal to another.

There is no separate operator for unequal to (<>). To evaluate a statement that contans the unequal to operator, the compiler uses the equal to operator (=), and negates the result.

As an example, the following opetrator allows to compare two complex numbers:

     operator = (z1, z2 : complex) b : boolean;

the above definition allows comparisions of the following form:

     Var
       C1,C2 : Complex;

     begin
       If C1=C2 then
         Writeln('C1 and C2 are equal');
     end;

The comparision operator definition needs 2 parameters, with the types that the operator is meant to compare. Here also, the compiler doesn’t apply commutativity; if the two types are different, then it necessary to define 2 comparision operators.

In the case of complex numbers, it is, for instance necessary to define 2 comparsions: one with the complex type first, and one with the real type first.

Given the definitions

     operator = (z1 : complex;r : real) b : boolean;
     operator = (r : real; z1 : complex) b : boolean;

the following two comparisions are possible:

     Var
       R,S : Real;
       C : Complex;

     begin
       If (C=R) or (S=C) then
        Writeln ('Ok');
     end;

Note that the order of the real and complex type in the two comparisions is reversed.

[prev] [prev-tail] [front] [up]