| |
| Posted by Stewart Gordon | PermalinkReply |
|
Stewart Gordon
| I've noticed this, while looking through the Phobos source (DMD 0.128):
std\typeinfo\ti_C.d
int equals(void *p1, void *p2)
{
Object o1 = *cast(Object*)p1;
Object o2 = *cast(Object*)p2;
return o1 == o2 || (o1 && o1.opCmp(o2) == 0);
}
Where on earth did this come from? It checks whether they're equal, and then if they're not, whether they rank equally in order.
The spec doesn't indicate whether having a class where unequal objects can rank equally in order (something that the Java API spec allows) is a correct practice in D.
But either way, something's wrong:
- If equal ranking of unequal objects is supposed to be allowed, then this function doesn't check objects for equality as its name (and presumably intended use) indicates.
- If it isn't meant to be allowed (and if so then this needs to be documented), then getting a second opinion on whether the objects are equal is a mere waste of time.
In either case, replacing the return statement with
return o1 == o2;
will fix it to this extent.
Moreover, I also noticed that while TypeInfo_C.compare treats null object references as a special case, TypeInfo_C.equals will just AV if o1 is null. This looks inconsistent and probably not what was intended. To fix:
return (o1 is o2) || (o1 !is null && o2 !is null && o1 == o2);
Stewart.
--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:- 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.
|