0

I'm trying to understand better the function is_equal() defined in class COMPARABLE.

I would like to know in which case, for an object o the function call o.is_equal(o) gives false.

I know that this function, differently from =, not just compares the address of objects, but also the values of all their attributes.

Following this definition I was pretty sure that such a call would always give true, since we're comparing the same object.

I would really appreciate if someone could explain me what's wrong in my thinking.

DevX10
  • 483
  • 2
  • 6
  • 16

1 Answers1

1

At the moment an origin of feature is_equal is class ANY, not COMPARABLE. Class COMPARABLE redefines the feature and adds a postcondition trichotomy that specifies properties of equality in terms of inequality (or, in terms of a query is_less to be more precise). So to understand what's going on, let's look at the origin.

Class ANY has an invariant

reflexive_equality: standard_is_equal (Current)

At the same time feature is_equal has a postcondition

consistent: standard_is_equal (other) implies Result

Combining the two (even without knowing what is standard_is_equal) we can derive a new invariant

new_reflexive_equality: is_equal (Current)

that should be valid for every object. So as soon as o in the expression yields the same object all the time (e.g. if it is a variable, and not a function that returns different objects at every call), o.is_equal (o) should always produce True. Of course, one can try to redefine is_equal to return False, but this would break the contract of the feature.

In real life the comparison is usually o1.is_equal (o2) that can be True or False depending on values of o1 and o2.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • Thanks a lot for the answer, the point is that in an old exam of Bertrand Meyer regarding Eiffel, the statement "For an object o, the feature call o.is_equal(o) always returns True" is false. – DevX10 Aug 20 '16 at 11:03
  • This means that there should be some cases where the statement is false, but I cannot find which it could be since I came at the same solution you came! – DevX10 Aug 20 '16 at 11:05
  • @Ergo: If that was really old, there could be no postcondition as in the current version and one could redefine `is_equal` to return `False` or even to.change the object under the hood. – Alexander Kogtenkov Aug 20 '16 at 17:23