-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Fix lengthCompare for indexed types #6930
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
Changes from all commits
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 |
---|---|---|
|
@@ -221,7 +221,7 @@ final class ArrayOps[A](val xs: Array[A]) extends AnyVal { | |
* x > 0 if this.length > len | ||
* }}} | ||
*/ | ||
def lengthCompare(len: Int): Int = xs.length - len | ||
def lengthCompare(len: Int): Int = Integer.compare(xs.length, len) | ||
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 would prefer if this reverted to the original behavior (i.e. it returns the difference in lengths) in case anyone is depending on it. Since we can count on
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. the default implementation never returned the difference in lengths, though. and if they happen to know it's an 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 I'm with @NthPortal on this, there is no reason or need to stick to a contract that we never documented. what we documented was to only rely on the sign 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. It's fair enough to say that we didn't agree to obey this contract so we're abandoning it, but I don't think it's hard to preserve it for |
||
|
||
/** Selects an interval of elements. The returned array is made up | ||
* of all elements `x` which satisfy the invariant: | ||
|
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.
what's the scenario where this fails? The test below has Int.MinValue, which seems impossible for a length...
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.
it passes in
Int.MinValue
as the value oflen
, which is perfectly possible. See this comment.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.
Well OK, but why would length be negative?
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.
because someone passed a negative number in ¯\_(ツ)_/¯
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.
@isaacl - As a concrete example, suppose you have a collection
xs
that you will use to pair with any overflow ofys
which is normally going to be paired withzs
. You might want to know ifxs
is big enough. So you askxs.lengthCompare(ys.length - zs.length)
.Beyond that, even if we can't think of a sensible use case for a negative value being passed in, it still should return the logically correct result, because we can't always anticipate what someone might try to use it for.
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.
OK. Well, this function doesn't feel super useful to me, but I guess if there's no preconditions it should return correctly.