https://issues.dlang.org/show_bug.cgi?id=14804
Issue ID: 14804
Summary: Comparing two Nullables does not check if either is
null
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: phobos
Assignee: [email protected]
Reporter: [email protected]
Currently, comparing two Nullables does not check if either is null. If either
of them are, Nullable.get will assert.
import std.typecons;
void main()
{
Nullable!int n1 = 0;
Nullable!int n2;
Nullable!int n3;
assert(n1 == n2); //Nullable.get asserts
assert(n2 == n3); //Nullable.get asserts
}
Instead a custom opEquals should be implemented that checks if either Nullable
is null before comparing their values. They should behave as shown below:
Nullable!int n4 = 0;
assert(n1 == n2); //n2 is null; returns false
assert(n2 == n3); //n2 and n3 are null; returns false
//Both n1 and n4 and non-null, so compare their values;
//Returns true
assert(n1 == n4);
--