www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Missing comparison operators

reply Gregor Richards <Richards codu.org> writes:
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
Sep 28 2007
next sibling parent BCS <ao pathlink.com> writes:
Reply to Gregor,


 <  >  =  na op
 F  F  F  F  !<>=(!<>=)
 T  T  T  T  !!<>=(!<>=)
these already exist: the syntax for the first is "false" the second "true". But seriously, what would they do?
Sep 28 2007
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 9/29/07, Gregor Richards <Richards codu.org> wrote:
 bool didThisCompare(T)(T a, T b, bool performComparison)
 {
      if (performComparison) {
          bool result = (a == b);
          return true;
      } else {
          return false;
      }
 }
I don't get it. The line "bool result = (a == b);" can surely be optimized away as it has absolutely no effect. Following that optimization, the rest of the code turns into bool didThisCompare(T)(T a, T b, bool performComparison) { if (performComparison) return true; else return false; } ...which is the same as... bool didThisCompare(T)(T a, T b, bool performComparison) { return (performComparison) ? true : false; } ...which is the same as... bool didThisCompare(T)(T a, T b, bool performComparison) { return performComparison; }
Sep 28 2007