-
-
Notifications
You must be signed in to change notification settings - Fork 703
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
Added opEquals to Nullable(T, T nullValue) #6583
Changes from all commits
e938ed0
992dee1
e014ad4
f973fb8
a81b9b5
e3bb226
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 |
---|---|---|
|
@@ -3431,6 +3431,32 @@ Params: | |
assert(!npi.isNull); | ||
} | ||
|
||
/** | ||
If they are both null, then they are equal. If one is null and the other | ||
is not, then they are not equal. If they are both non-null, then they are | ||
equal if their values are equal. | ||
*/ | ||
bool opEquals()(auto ref const(typeof(this)) rhs) const | ||
{ | ||
return _value == rhs._value; | ||
} | ||
|
||
/// Ditto | ||
bool opEquals(U)(auto ref const(U) rhs) const | ||
if (is(typeof(this.get == rhs))) | ||
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 doesn't seem right. Shouldn't it be:
? 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. That is possible, it's taken from Will change it for both if that would be correct. Will wait for a more official answer to that. 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. No, it's right, it's asking can You wouldn't want the other way, because then e.g. 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. I think this is correct, but it may be surprising in some cases. For example: enum NullValue = int.max;
Nullable!(int, NullValue) n;
assert(n == NullValue); // fails However, I also don't know that we want to make this true either. I think the proposed behavior is at least consistent with the other Nullable. 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. At least should be 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. Can you explain the ambiguity? I'm always lost when it comes to the differences between 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. I mean, to avoid the confusion with
( 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. But there are subtle differences. I don't remember what they are, but someone (maybe Timon or Stefan?) has outlined why you should prefer one or the other, I don't have the link to the NG post handy. I'd prefer leaving it the way it is for this PR, since it's already that way for the other version of |
||
{ | ||
return isNull ? false : rhs == _value; | ||
} | ||
|
||
@system unittest | ||
{ | ||
Nullable!(uint, uint.max) val3; | ||
Nullable!(uint, uint.max) val4; | ||
assert(val3.isNull); | ||
assert(val4.isNull); | ||
assert(val3 == val4); | ||
} | ||
|
||
/** | ||
Gets the value. `this` must not be in the null state. | ||
This function is also called for the implicit conversion to `T`. | ||
|
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.
Ugh, I hate to do this to you, but actually there needs to be one static if: if
_value
is a floating point, then this won't work, butisNull
will. So you need to duplicate whatisNull
does in that instance.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.
Or make null == null implementation defined? If one really needs to compare nulls, let him do so manually.
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.
We can just do: