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

How to annotate Collection.toArray(T[]) #481

Open
kevinb9n opened this issue Feb 12, 2024 · 2 comments
Open

How to annotate Collection.toArray(T[]) #481

kevinb9n opened this issue Feb 12, 2024 · 2 comments
Labels
library-use-case Question of how a particular library should be annotated; could potentially affect design or docs nullness For issues specific to nullness analysis.

Comments

@kevinb9n
Copy link
Collaborator

Currently using the signature:

<T extends @Nullable Object> T[] toArray(T[])

Originally posted by @cpovirk in #65 (comment)

Concretely, it seems like the alternatives would be:

1:

<T> @Nullable T[] toArray(@Nullable T[])

I'd already said a little about that alternative above, but: The theory would be that, because the collection might contain nulls or your array might be too large, we might end up inserting a null into the array no matter what. So we at least give it back to you as an array of nullables. But of course you might pass an input array of non-nullables and then choose to use it afterward. That would be unsound but nothing that you couldn't have done to yourself already through array covariance if you had wanted to.

The downside is the inconvenience of the array-of-nullables return type when you know it's not necessary.

2:

<T> T[] toArray(@Nullable T[])

With this signature, we'd be allowing you to pass any array type but then give it back to you as an array of non-nullables, thereby giving you the option to either use that type (if you know it's safe) or re-widen it (if not).

The downside is that you might go to the trouble of passing an array of nullables to be safe but then not realize that the nullability is lost along the way.

3:

<T> T[] toArray(T[])

This is dangerous is ways similar to (2) but even more inconvenient for someone who is trying to do the right thing and pass a nullable array. I guess the case for it would rest on the idea that most collections don't contain nulls and most inputs are only type tokens, so why complicate the picture with nullness, especially when covariant arrays throw it all out the window?

(I think I see even less case for a potential (4) <T> @Nullable T[] toArray(T[])? Like, if you're arguing that the return type needs to reflect that the array might have nulls in it, then why not even optionally allow the input type to reflect that, too?)

(Maybe I'm overlooking other signatures in which <T extends @Nullable Object> could be useful?)

Does any of that strike your fancy? I think I'm still happy with the current signature, but I'm not dead certain.

@kevinb9n kevinb9n added the design An issue that is resolved by making a decision, about whether and how something should work. label Feb 12, 2024
@kevinb9n
Copy link
Collaborator Author

(So far I've been treating issues like this (how to annotate certain libraries) as though they are proper JSpecify design questions. The idea being that how we resolve this could have an effect on how we specify or even just document things.)

@kevinb9n kevinb9n added nullness For issues specific to nullness analysis. library-use-case Question of how a particular library should be annotated; could potentially affect design or docs and removed design An issue that is resolved by making a decision, about whether and how something should work. labels Mar 13, 2024
@kevinb9n kevinb9n added this to the 1.0 milestone Mar 14, 2024
@Mooninaut
Copy link

I don't see any option but 1. as being viable without a way to statically distinguish between a null-safe zero-length array argument and a null-unsafe positive-length array. If you want a T![]! from a Collection<T!> you can and should use the toArray(IntFunction<T[]> generator) overload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
library-use-case Question of how a particular library should be annotated; could potentially affect design or docs nullness For issues specific to nullness analysis.
Projects
None yet
Development

No branches or pull requests

3 participants