AA with complex keytype?

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Fri Feb 9 08:31:23 PST 2007


Manfred Nowak wrote:
> Frits van Bommel wrote
>> As long as the order is consistent[1] (a < b && b <
>> c => a < c, that sort of thing) they're happy.
> 
> Thank you. This answers my question.
> 
> But immediately another arises:
> Why are classes required to implement opCmp( Object) for AA's to 
> function properly?

Because the TypeInfo for objects uses Object.opCmp (if neither is null)

> Currently AA's misbehave if opCmp(Object) is not overwritten for a 
> class, but no error is thrown.
> 
> For the transitivity of an ordering it suffices for opCmp(Object) to 
> always give "==" if the objects are equal, and give "<" if they are 
> not.

By "that sort of thing", I meant "and similar properties". For instance, 
the order also needs to be non-symmetric: a < b => !(b < a)[1]. Your 
proposed default implementation doesn't satisfy that.

IIRC Object used to compare addresses by default, but that was 
problematic because that behavior disallows moving garbage collectors.

> It should be easy to implement this as the default opCmp(Object).

But as explained above it wouldn't work correctly...



[1]: Actually, since the compare return value can have three meanings 
(less than, equal to, greater than), that would be: a < b => b > a



More information about the Digitalmars-d mailing list