Ada 95 Quality and Style Guide | Chapter 5 |

5.7.5 Overloading the Equality Operator

guideline

Define an appropriate equality operator for private types.

Consider redefining the equality operator for a private type.

When overloading the equality operator for types, maintain the properties of an algebraic equivalence relation.

rationaleThe predefined equality operation provided with private types depends on the data structure chosen to implement that type . If access types are used, then equality will mean the operands have the same pointer value. If discrete types are used, then equality will mean the operands have the same value. If a floating- point type is used, then equality is based on Ada model intervals (see Guideline 7.2.7). You should, therefore, redefine equality to provide the meaning expected by the client. If you implement a private type using an access type, you should redefine equality to provide a deep equality. For floating-point types, you may want to provide an equality that tests for equality within some application-dependent epsilon value.

Any assumptions about the meaning of equality for private types will create a dependency on the implementation of that type. See Gonzalez (1991) for a detailed discussion.

When the definition of "

=" is provided, there is a conventional algebraic meaning implied by this symbol. As described in Baker (1991), the following properties should remain true for the equality operator:

- - Reflexive:
a = a

- - Symmetric:
a = b ==> b = a

- - Transitive:
a = b and b = c ==> a = c

In redefining equality, you are not required to have a result type of

Standard.Boolean. The Rationale (1995, §6.3) gives two examples where your result type is a user-defined type. In a three-valued logic abstraction, you redefine equality to return one ofTrue,False, orUnknown. In a vector processing application, you can define a component-wise equality operator that returns a vector of Boolean values. In both these instances, you should also redefine inequality because it is not the Boolean complement of the equality function.

< Previous Page |
Search | Contents | Index | Next Page > |

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |

TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC |

Appendix | References | Bibliography |