Fuzion Logo
flang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

Equality

An equality operation is a predicate on two values of the same type, typically written as a = b. Ideally, it partitions the values into disjoint equivalence classes. For this, the predicate has to be reflexive, i.e.,

a = a,

symmetric, i.e.,

a = b ⟺ b = a,

and reflexive, i.e.,

a = b ∧ b = c → a = c,

must hold for all a, b and c.

In object-oriented languages, the relation of equality and inheritance is not obvious.

Kinds of equality

There are basically three kinds of equality

A total order ≤ defines an equality relation as follows

a = b :⟺ a ≤ b ∧ b ≤ a,

while a partial order does not. It is therefor important to define an equality operation in a way that it could easily be extended to or derived from a total order.

Other languages

Java

Reference equality

The Java infix operator == performs a check for reference equality. This goes awfully wrong for code such as

(Integer) i == (Integer) i

that results in true for some values of i and false for others.

Object.equals()

This is a real mess. Typical implementations of equals consist of an instanceof check of the argument and a comparison of fields in case it is the same instance. The result is that equals is generally not symmmetric in case of several redefinitions of equals along the inheritance tree

Comparator

Comparator is a generic interface in Java that compares two instances of the type given as a generic argument. This allows the definition of several equality relations for different types at different levels in an inheritance tree. It is generally more flexible and allows different equivalence relations to be defined for the same type, but it is more cumbersome to use than Object.equals().

Literature

Equality in Fuzion

Guiding principles:

Approach: TBD!