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
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.