Chapter 7. Operator Redefinition

Table of Contents
7.1. Method Names for Operators
7.2. Operator expressions
7.3. Array Access Routines

7.1. Method Names for Operators

It is possible to define operators such as + and * to work with objects of arbitrary classes. These operators are transformed into standard routine calls in the class. Thus, if a class defines the routine 'plus' you can then apply the + operator to objects from that class. For instance, the complex number class POINT could define the plus routine to mean pairwise addition
class POINT is
   readonly attr x,y:INT;
   create(x,y:INT):SAME is ... -- same as before
   plus(s:POINT):POINT is
      return #POINT(x + s.x, y + s.y);
   end;

we can now use the plus routine on two points
p1:POINT := #POINT(3,5);
p2:POINT := #POINT(4,6);
p3:POINT := p1 + p2;  --  p3 is set to the point 7,11

Most of the standard operators may be redefined; in some cases, redefining one operator such as the < operator implicitly redefines the associated >, >= and <= operators. These operators are meant to be used together in a consistent manner to indicate the mathematical notion of complete or partial ordering. They are not intended to be used as a convenient short-hand for other purposes.