Missing comparison operators
Gregor Richards
Richards at codu.org
Fri Sep 28 17:01:47 PDT 2007
I was perusing the operators page, and noticed that all of the
comparison operators are listed by whether they return true for
particular possibilities, namely less than, greater than, equal and
incomparable (unordered). They return true if any of the possibilities
marked 'T' are true.
Since each operator can return true or false in each of these four
possibilities, there are clearly 2^4 == 16 possibilities for operators.
But, only 14 exist, listed here:
< > = na op
F F F T !<>=
F F T F ==
F F T T !<>
F T F F >
F T F T !<=
F T T F >=
F T T T !<
T F F F <
T F F T !>=
T F T F <=
T F T T !>
T T F F <>
T T F T !=
T T T F <>=
Clearly, there are missing possibilities: Namely FFFF and TTTT. Here are
my suggestions for the operators, with a brief explanation of their purpose:
< > = na op
F F F F !<>=(!<>=)
T T T T !!<>=(!<>=)
These operators are the same underlying question: "is A being compared
to B?" The difference is that !<>=(!<>=) does not compare A to B, and
therefore returns false, whereas !!<>=(!<>=) does compare A to B
(although it then disregards the result), and so it returns true.
If I wanted to implement this operation now, I would need a function
like this:
bool didThisCompare(T)(T a, T b, bool performComparison)
{
if (performComparison) {
bool result = (a == b);
return true;
} else {
return false;
}
}
The lack of these operators is clearly a consistency problem with the
language. Any combination of {<, >, =, na} should be comparable, not
just some elite few. In my opinion, this consistency problem shows a
design flaw in D: Just because you don't have an immediate use for
something doesn't mean you shouldn't include it to be consistent.
- Gregor Richards
More information about the Digitalmars-d
mailing list