07 April 1998
Here's the question again:
Should this instruction print "true" or "false"?
print( (1).max(1) = (1).max(1) )
It will print "false", because the comparison is between two references, not two values. Here's why:
Function 'max' is defined in deferred class COMPARABLE:
max(other: like current): like current
-- The greater of current object and 'other'
require
other_exists: other /= void
ensure
current_if_not_smaller: (current >= other) implies (result = current)
other_if_smaller: (current < other) implies (result = other)
The anchored declarations ("like current") cause automatic redefinition in descendant classes. So, we can use 'max' with INTEGERs, CHARACTERs etc because these are descendants of COMPARABLE.
But the catch is that anchored types are always reference types. ETL 2nd printing page 215 states:
...the base type of an entity of type "like current" in a class C does not include the keyword expanded even if class C is expanded. As a result, an entity of anchored type will always denote a reference.
So, (1).max(1) returns an INTEGER_REF containing the INTEGER 1, and when we test (1).max(1)=(1).max(1) we are comparing two object references, which of course are not equal. The problem goes away if we use 'is_equal' to test for value equality:
print( (1).max(1).is_equal((1).max(1)) )
Thanks to Alexander Kogtenkov of SIG Moscow for bringing this one to my attention.
Eiffel and NICE are registered trademarks of the Nonprofit International Consortium for Eiffel.