int opEquals(Object), and other legacy ints

Stewart Gordon smjg_1998 at yahoo.com
Sun Jul 30 15:57:04 PDT 2006


Walter Bright wrote:
> Deewiant wrote:
>> Walter Bright wrote:
>>> Stewart Gordon wrote:
>>>> Walter Bright wrote:
>>>>> I can. (a == b), where a and b are ints, can be implemented as (a -
>>>>> b), and the result is int 0 for equality, int !=0 for inequality.
>>>> How is this (a == b) rather than (a != b)?
>>> I don't understand your question.
>>
>> (a - b), if a and b are equal ints, evaluates to 0, which is generally
>> considered to mean false. So isn't (a - b) actually a way of finding 
>> (a != b),
>> instead of (a == b)?
> 
> Oh, I see what you mean.
> 
> To invert the result would take another 2 instructions for a total of 3, 
> still less than 4.

Exactly.  But because what we have is opEquals and not opNotEquals, the 
benefit of fewer instructions is lost (except when opEquals is simple 
enough that the compiler can inline and optimise away the double negation).

Indeed, on this basis, if we had opNotEquals then it would be just be 
equivalent to opCmp for many types.  So I can see people thinking that 
opNotEquals should just call opCmp by default.  However, there's a 
problem with this idea - for classes that have no ordering, even the 
current behaviour of comparing object references would have to be 
explicitly programmed in.

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- 
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.



More information about the Digitalmars-d-bugs mailing list