0

In Eiffel, if comparing objects of an expanded type, the = operator compares them field by field, checking if the content of each field is identical in both objects.

Let's imagine two expanded classes with no features defined in them:

expanded class A
end

expanded class B
end

How can Eiffel tell them apart? Or can't it? Does it have to do with some field(s) inherited from ANY?

both_are_equal: BOOLEAN
    local
        a: expanded A
        b: expanded B
    do
        Result := a = b
    end
g4v3
  • 133
  • 2
  • 10

1 Answers1

1

The field-by-field comparison is applied only when both objects are of the same type. If they are of different types the equality operator gives false. In other words, the equality operator = for expanded types is identical to the equality operator ~ with the semantics

type_of (a) = type_of (b) and then a.is_equal (b)

Therefore both_are_equal will give False.

The result will be the same if instead of a and b of expanded types there will be x and y of reference types attached to expanded objects - the comparison takes types of objects into account:

both_are_equal: BOOLEAN
    local
        a: expanded A
        b: expanded B
        x: ANY
        y: ANY
    do
        x := a
        y := b
        Result := x = y -- Gives False
    end

But if the reference expressions are attached to expanded objects of the same type, field-by-field comparison is used, not reference equality:

both_are_equal: BOOLEAN
    local
        a: expanded A
        b: expanded A -- Note the type change
        x: ANY
        y: ANY
    do
        x := a
        y := b
        Result := x = y -- Gives True even though x and y are different objects
    end

Some details on equality operators can be found in the Standard ECMA-367 (section 8.21) and in contracts of specific comparison features in the class ANY.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • Thank you for the answer! Could you please just add a link to some documentation for further reading? – g4v3 Nov 18 '16 at 21:05
  • @g4v3, I've added a couple of links at the end of the answer. At the moment the standard is a bit outdated, but main points are still valid. The link to the source code of the class `ANY` is completely up-to-date. – Alexander Kogtenkov Nov 19 '16 at 13:18