www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - opCmp just for AA's

reply Michiel Helvensteijn <nomail please.com> writes:
At the moment, if you wish your own struct/class to serve as keytype for
associative arrays, you have to provide the opCmp function, among others.

There is one drawback to this. It will also allow users of the class to use
the inequality operators on its instances. This is not always wanted.

For example: A point in Euclidean space is mathematically not greater or
smaller than another. Thus it would not be appropriate to use the <
operator to compare two points. And so it should not be possible. It's
perfectly reasonable to use a point as a keytype, though. But at the
moment, in making that possible, you also define the inequality operators.

In my opinion, there should be two types of compare functions:

* opCmp() as it exists now. This defines the inequality operators.
* cmp(), which should only be used in data-structures, to compare keys.

If opCmp() exists, but cmp() does not, calling cmp() should automatically
call opCmp(). But not the other way around.

Opinions?

-- 
Michiel
Mar 12 2007
parent Manfred Nowak <svv1999 hotmail.com> writes:
Michiel Helvensteijn wrote

 It will also allow users of the
 class to use the inequality operators on its instances.

True, but only with casting to Object. In effect the opCmp, that has to be provided for AA's has a different signature and can be functionally different from the opCmp, that has to be provided for comparisons. -manfred
Mar 12 2007