Skip to content

equality expression

Cameron Purdy edited this page Apr 7, 2020 · 1 revision

Comparison for purposes of equality uses the well-known expression forms:

a == b
a != b

The type of the expressions a and b must both be compatible for purposes of equality. In the simplest case, this means that there must be a bi-implicit type T of the expressions a and b, and that type T must have an equals() function that matches a specific signature pattern[1]:

static <CompileType extends T> Boolean equals(CompileType o1, CompileType o2)

Additionally, Nullable types are treated as a special case for this expression; it is permissible to compare a Nullable | T with a T, or vice-versa. In a situation in which one or both of the references may be Null, the result of the comparison is False if exactly one of the two references is Null.

As in many scenarios in which known types can be used to infer unknown types, the type of expression a is used to infer the type of the expression b, such as in the following example:

enum Color {Red, Green, Blue};
Color c = chooseAnyColor();
// because the left-hand type is of type Color, the right-hand
// side does not need to be qualified
if (c == Red)
    {
    c = Blue;
    }

The implicit type of the expression is Boolean.

The expression short-circuits if either expression a or b short-circuits.

The expression uses the default left-to-right definite assignment rules:

  • The VAS before a is the VAS before the expression.
  • The VAS before b is the VAS after a.
  • The VAS after the expression is the VAS after b.

The EqualityExpression groups to the left, so a == b == c is treated as (a == b) == c:

    EqualityExpression:
        RelationalExpression
        EqualityExpression == RelationalExpression
        EqualityExpression != RelationalExpression

[1]

Since Object declares an equals() function, all types implicitly support comparison for purpose of equality.