Perfect Developer intermediate tutorial 7 This page last modified 2011-10-31 (JAC)

Declaring Comparison Operators

A special sort of operator declaration is a declaration of the comparison operator. This operator is named "~~" and takes an operand whose type is implicitly the type of the class in which it is declared (so the type is not given explicitly). Likewise, the result is always of type rank so the result type is never declared. Here is the declaration of a comparison operator for class Money:

  total operator ~~ (other)
    ^= amount ~~ other.amount;

Here we are making use of the fact that "~~" is already defined for class int (as it is for the other built-in classes). The reason for the total keyword is that we wish to declare that this operator declaration defines a total ordering on Money objects. In other words, if two Money objects are not equal, one of them must be greater than the other.

When might we not want to define a total ordering? When we have no particular reason to compare all the attributes of the objects concerned! For example, support we wish to define a comparison operator on class Book such that we can sort a sequence of books into title order. In the event of two Book objects having the same title but different authors, published versions or prices, we don't care which comes first. So we will only compare titles, giving us the following operator declaration in class Book:

  operator ~~ (other)
    ^= title ~~ other.title;

Since two books with the same title but different authors will be unequal but compare 'same', this operator does not define a total ordering; hence we do not use the total keyword.

What if we do want to define a total ordering for class Book? Let's say that when the titles compare same, we will order books by the author list.

We can achieve this using the following definition:

  operator ~~ (other)
    ^= ( let titleCompare ^= title ~~ other.title;
         [titleCompare = same@rank]:
           authors ~~ other.authors,
         []:
           titleCompare
       );

However, this is still not a total ordering, because we could have two Book objects with the same title and authors but different published versions or prices. Extending the declaration to provide a total ordering is left as an exercise for you.

Once the "~~" operator has been declared in a class, as well as using the "~~" operator between objects of that class, you can also use the binary ">" (greater-than) and "<" (less-than) operators and their negated versions.

For example, the expression myBook < yourBookis equivalent to (myBook ~~ yourBook) = below@rank.

If the operator is declared total, you can use ">=" and "<=" as well.

For example, myMoney <= yourMoney is equivalent to (myMoney ~~ yourMoney) ~= above@rank.

Next:  Verifying specifications

 

Save My Place Glossary Language Reference Manual
Tutorials Overview Main site   
Copyright © 1997-2012 Escher Technologies Limited. All rights reserved. Information is subject to change without notice.