-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Don't override neqv in Order #2230
Closed
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Eq provides a reqsonable default implementation of `neqv`: ```scala def neqv(x: A, y: A): Boolean = !eqv(x, y) ``` Before this change, `Order` overrides this default implementation as: ```scala compare(x, y) != 0 ``` I think that this override is questionable for a few reasons: - The negation of `eqv` seems like the most straightforward implementation of `neqv` to me - It seems like an oversight that `Order` overrides `neqv` but `PartialOrder` does not. - If someone is overriding `eqv` on an `Order` instance, presumably they are doing it because their `eqv` implelentation can be faster than doing a full `compare`. If this is the case and they don't override `neqv`, it would be better for them to inherit a !eqv` implementation. As a sidenote, the current implementation leads to a lack of symmetry between `===` and `=!=` in the presence of `null`. `"foo" === null` returns `false` while `"foo" =!= null` throws an NPE. I generally agree with the decision in Cats to not perform special-handling of `null`. However, the way that I ran into this was when adding a linting rule that forbids non type-safe equality (== and !=), and it is kind of a bummer that if you are working with a third-party library that might return `null` and you want to safeguard against that you can't just check for `x =!= null` instead of `x != null` to satisfy the linting rule. You can do something like `Option(x).isDefined`, but that introduces an extra allocation and seems a little roundabout to me.
LukaJCB
approved these changes
Apr 16, 2018
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.
LGTM!
👍 |
Oh this isn't binary compatible in Scala 2.10 and 2.11 :( |
To get around the binary compatibility issue, I guess that I could keep the |
ceedubs
added a commit
to ceedubs/cats
that referenced
this pull request
Apr 17, 2018
Based on the discussion in typelevel#2230.
Replaced by #2232 |
kailuowang
pushed a commit
that referenced
this pull request
Apr 17, 2018
Based on the discussion in #2230.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Eq provides a reasonable default implementation of
neqv
:Before this change,
Order
overrides this default implementation as:I think that this override is questionable for a few reasons:
eqv
seems like the most straightforward implementation ofneqv
to meOrder
overridesneqv
butPartialOrder
does not.
eqv
on anOrder
instance, presumably they aredoing it because their
eqv
implelentation can be faster than doing a fullcompare
. If this is the case and they don't overrideneqv
, it would bebetter for them to inherit a
!eqv
implementation.As a sidenote, the current implementation leads to a lack of symmetry
between
===
and=!=
in the presence ofnull
."foo" === null
returns
false
while"foo" =!= null
throws an NPE. I generally agreewith the decision in Cats to not perform special-handling of
null
.However, the way that I ran into this was when adding a linting rule
that forbids non type-safe equality (== and !=), and it is kind of a
bummer that if you are working with a third-party library that might
return
null
and you want to safeguard against that you can't justcheck for
x =!= null
instead ofx != null
to satisfy the lintingrule. You can do something like
Option(x).isDefined
, but thatintroduces an extra allocation and seems a little roundabout to me.