Skip to content

Comments

Fix issue #13017: opEquals for null std.typecons.Nullable#5032

Merged
JackStouffer merged 1 commit intodlang:masterfrom
jmdavis:issue_13017
Jan 14, 2017
Merged

Fix issue #13017: opEquals for null std.typecons.Nullable#5032
JackStouffer merged 1 commit intodlang:masterfrom
jmdavis:issue_13017

Conversation

@jmdavis
Copy link
Member

@jmdavis jmdavis commented Jan 13, 2017

I was getting sick of this not working, and there have been multiple bug
reports on this, so I'm just fixing it. With these changes, using == and
!= on null Nullables will not result in an AssertError but instead will
consider them equal if they're both null, equal if they're both non-null
and have the same value, and not equal otherwise.

@dlang-bot
Copy link
Contributor

Fix Bugzilla Description
13017 opEquals for null std.typecons.Nullable

std/typecons.d Outdated
*/
bool opEquals()(auto ref inout(typeof(this)) rhs) inout
{
if(_isNull)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code style is to have a whitespace after if (that's why CircleCi is failing)

Btw it's also not covered by the unittests.

Copy link
Contributor

@JackStouffer JackStouffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicks

return rhs._isNull;
if(rhs._isNull)
return false;
return _value == rhs._value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of accessing a pseudo private member, why not return _value == rhs.get;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you do that? This is opEquals. It's normal to access the member variables of the two variables to compare them. Calling get would just add unnecessary overhead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you're accessing an undocumented public member, something that we're trying to move away from and make these members private. _value should really be private or package.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is private. And since this is a member function, it's accessible. The fact that it's from another instance doesn't matter. The normal thing to do in a function like opEquals or opCmp is to use the member variables directly, and it's often the case that that's the only way that they can be used, because they're private. It would be incredibly bizarre to call a member function to access a member to compare it in opEquals rather than just accessing it directly.

Nullable!int b = 42;
Nullable!int c = 27;

assert(empty == empty);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For documentation sake, I would add a line here that says assert(empty.isNull);

//`get` is implicitly called. Will throw
//an AssertError in non-release mode
assertThrown!Throwable(ni == 0);
assertThrown!AssertError(i = ni);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

==?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. == never asserts now, whereas = does.

@jmdavis
Copy link
Member Author

jmdavis commented Jan 14, 2017

Fixed whitespace.

@JackStouffer
Copy link
Contributor

the codecov error looks like a coverage bug

Copy link
Member

@PetarKirov PetarKirov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, aside from one small nit.

std/typecons.d Outdated
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 inout(typeof(this)) rhs) inout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: const is more appropriate than inout, since you don't depend on the mutability in any way.

I was getting sick of this not working, and there have been multiple bug
reports on this, so I'm just fixing it. With these changes, using == and
!= on null Nullables will not result in an AssertError but instead will
consider them equal if they're both null, equal if they're both non-null
and have the same value, and not equal otherwise.
@JackStouffer
Copy link
Contributor

Auto-merge toggled on

@JackStouffer JackStouffer merged commit d94442c into dlang:master Jan 14, 2017
@jmdavis jmdavis deleted the issue_13017 branch January 20, 2017 12:06
@CyberShadow
Copy link
Member

This pull request introduced a regression:
https://issues.dlang.org/show_bug.cgi?id=17482

@WalterBright
Copy link
Member

@jmdavis ping, please look at the regression!

@jmdavis
Copy link
Member Author

jmdavis commented Jul 5, 2017

#5541

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants