Search code examples
comparecomparisoneiffel

Eiffel is_equal() function


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.


Solution

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