opCmp just for AA's

Michiel Helvensteijn nomail at please.com
Mon Mar 12 14:52:18 PDT 2007


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




More information about the Digitalmars-d mailing list