Skip to content
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

Annotate the parameter of List.sort as @Nullable. #13

Merged
merged 2 commits into from
Jun 7, 2023
Merged

Conversation

cpovirk
Copy link
Collaborator

@cpovirk cpovirk commented Jun 7, 2023

This has always struck me as a weird feature, but people use it, and it
"works" in the sense of "does not cause NPE" (though it may cause
CCE...). I see both calls to sort(null) and calls like
sort(priorityQueue.comparator()) (which might be null;
example).
And while I prefer a world in which methods like comparator() never
return null, as we arranged for in
SortedMultiset
, there
are apparently downsides
to using a Comparator that implements natural order rather than using
null.

Is any of that convincing? :) This is definitely a case in which I can
see how eisop would want to stay on its own path. My main motivation for
doing this now is that I hear that the current signature is causing us
trouble during Java->Kotlin transpilation. I can get more details.

This has always struck me as a weird feature, but people use it, and it
"works" in the sense of "does not cause NPE" (though it may cause
CCE...). I see both calls to `sort(null)` and calls like
`sort(priorityQueue.comparator())` (which _might_ be `null`;
[example](https://github.com/google/guava/blob/e82e2a2c07c68108f318958ee0355cc835c97743/guava-testlib/src/com/google/common/collect/testing/testers/SortedSetNavigationTester.java#L57)).
And while I prefer a world in which methods like `comparator()` never
return `null`, as we arranged for [in
`SortedMultiset`](https://guava.dev/SortedMultiset#comparator()), there
are apparently [downsides](google/guava#6187)
to using a `Comparator` that implements natural order rather than using
`null`.

Is any of that convincing? :) This is definitely a case in which I can
see how eisop would want to stay on its own path. My main motivation for
doing this now is that I hear that the current signature is causing us
trouble during Java->Kotlin transpilation. I can get more details.
Copy link
Collaborator

@wmdietl wmdietl left a comment

Choose a reason for hiding this comment

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

This makes perfect sense. The javadoc for that parameter explicitly says that null is a valid value.
The default implementation in List calls Arrays.sort, which is already properly annotated:

public static <T extends @Nullable Object> void sort(T[] a, @Nullable Comparator<? super T> c) {

I see no reason to reject this from EISOP and would appreciate a PR.

@cpovirk
Copy link
Collaborator Author

cpovirk commented Jun 7, 2023

Whoa, I did not even realize the Arrays precedent. Thanks!

@cpovirk cpovirk merged commit a227f7d into main Jun 7, 2023
@cpovirk cpovirk deleted the listsort branch June 7, 2023 22:06
cpovirk added a commit to cpovirk/jdk that referenced this pull request Jun 8, 2023
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.

2 participants