[Issue 323] New: Error: need opCmp for class Bar

Sean Kelly sean at f4.ca
Thu Sep 7 08:18:50 PDT 2006


Since D's associative arrays use opCmp to resolve collisions, any class 
that has opEquals defined should probably have opCmp defined also.  The 
old default implementation of Object.opCmp in Phobos was not compatible 
with compacting GCs so it was replaced with a version that throws an 
exception on use.  However, there is a default implementation that 
actually works and is compatible with compacting GCs:

     int opCmp(Object o)
     {
	return this !is o;
     }

Compare to opEquals:

     int opEquals(Object o)
     {
         return this is o;
     }

The opCmp function above forces the binary tree chaining mechanism into 
a linked-list, and equivalence is resolved by pointer equality rather 
than some ordering mechanism.  It won't be as fast as an ordering 
implementation of opCall for the degenerate case but it has the benefit 
of being immune to object movement.  If a default implementation of 
opEquals is to remain in Phobos then I suggest adopting the default 
opCmp implementation above as well.



More information about the Digitalmars-d-bugs mailing list