[Issue 14804] Comparing two Nullables does not check if either is null

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Dec 26 18:32:57 PST 2015


https://issues.dlang.org/show_bug.cgi?id=14804

--- Comment #2 from monkeyworks12 at hotmail.com ---
I agree opCmp is a little weird to implement for Nullable, but it's really not
much different from NaN. If we follow what the floating point numbers do:

Nullable!int n1;
Nullable!int n2 = 0;

assert(!(n1 < n2) && !(n1 == n2) && !(n1 > n2));

Of course the semantics of NaN are somewhat confusing at first and possibly
bug-prone, so it may not be something we want to duplicate in another type if
possible. Interestingly, this is how the built-in "nullable" types behave when
compared:

    Object o1 = null;
    Object o2 = new Object();
    assert(o1 > o2); //Segfault

    int* i1 = null;
    int* i2 = new int(0);
    assert(i1 <= i2); //One of these will pass
    assert(i1 > i2);

    int[] a1 = null;
    int[] a2 = [0];
    assert(a1 <= a2); //I didn't know this was valid code;
    assert(a1 > a2);  //it must compare the array pointers
                          //and thus work similarly to case 2

So it looks like we have our choice of semantics to choose from.


Either way, the issue of opCmp is completely separate from opEquals, so I don't
agree that this bug should be closed. Every built-in nullable type in D works
as I described, and I don't believe that Nullable should be any different.

Object o1 = null;
Object o2 = new Object();
assert(o1 != o2); //Passes

int* i1 = null;
int* i2 = new int(0);
assert(i1 != i2); //Passes

int[] a1 = null;
int[] a2 = [0];
assert(a1 != a2); //Passes


The fact that these issues pop up when you actually try to use Nullable in any
serious way suggest to me that its design is deeply flawed, and that it should
be deprecated and replaced at some point. However, until that happens, we
should aim to improve it as much as possible without breaking existing code.

If you don't want to open a defect for Nullable.opCmp, that's fine with me, but
let's not close this one for opEquals.

--


More information about the Digitalmars-d-bugs mailing list