-
-
Notifications
You must be signed in to change notification settings - Fork 739
Implement opCmp for Nullable objects #10762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 8 commits
149465f
643a1b2
fb2102a
0ff0008
cb391a8
fe82e26
fb07e99
95a092b
6719043
cb8d3ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3675,6 +3675,59 @@ struct Nullable(T) | |
|
||
private bool _isNull = true; | ||
|
||
/** | ||
* Compares two Nullable values. | ||
* - If one is null and the other is not, the null one is considered smaller. | ||
* - If both are null, they are equal. | ||
* - If both are non-null, compares the payloads. | ||
* | ||
* Returns: | ||
* Negative if `this < rhs`, zero if equal, positive if `this > rhs`. | ||
*/ | ||
int opCmp(this This, Rhs)(auto ref Rhs rhs) const | ||
if (is(typeof(_value.payload < rhs.get)) && is(typeof(_value.payload > rhs.get))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test will return if (is(Rhs == Nullable!U, U) && is(typeof(this.get < rhs.get))) (Strictly speaking, it doesn't matter whether you use |
||
{ | ||
static if (is(Rhs == This)) | ||
DeterminedSage marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (_isNull) | ||
return rhs._isNull ? 0 : -1; | ||
else if (rhs._isNull) | ||
return 1; | ||
else | ||
return _value.payload < rhs._value.payload ? -1 : | ||
_value.payload > rhs._value.payload ? 1 : 0; | ||
} | ||
else | ||
{ | ||
static if (is(typeof(rhs.isNull))) | ||
{ | ||
if (_isNull) | ||
return rhs.isNull ? 0 : -1; | ||
else if (rhs.isNull) | ||
return 1; | ||
else | ||
return _value.payload < rhs.get ? -1 : | ||
_value.payload > rhs.get ? 1 : 0; | ||
} | ||
else | ||
{ | ||
return _isNull ? -1 : (_value.payload < rhs ? -1 : (_value.payload > rhs ? 1 : 0)); | ||
} | ||
Comment on lines
+3712
to
+3715
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This branch is unreachable and should be deleted. |
||
} | ||
} | ||
|
||
// Basic Null Comparison | ||
@safe unittest | ||
{ | ||
Nullable!int a = 5; | ||
Nullable!int b = Nullable!int.init; | ||
|
||
assert(a > b); | ||
assert(b < a); | ||
assert(a == a); | ||
assert(b == b); | ||
} | ||
|
||
/** | ||
* Constructor initializing `this` with `value`. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
typeof(this)
instead ofthis This
if you need the this pointers type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it is no longer needed, you can remove this template parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.