Repository navigation
Description
(Previously I gave an example predicated on using @NullnessUnspecified for Map.get. But there may be less support for that than I'd thought, and anyway, I think this is worth considering independent of that.)
Any generic type that isn't annotated for nullness is likely to have a method that we consider to return @NullnessUnspecified T. For example:
class Reference<T> {
T get();
}It seems pretty clear that, if I write Reference<@Nullable String> (probably receiving a warning), I expect for get() to return a @Nullable String.
But the design proposal currently has the following rule:
If the bound’s effective specifier is
@NotNullor@NullnessUnknown, then that is the type variable usage’s effective specifier.
So get() returns a @NullnessUnknown String. That's still pretty good, but I can still imagine that users would feel "cheated" here that we didn't respect the @Nullable annotation.
The purpose of the current rule is, as I understand it, more about @NotNull. We don't want to assume that an unannotated method that returns T is meant to return exactly T as opposed to @Nullable T. (@Nullable T, incidentally, would be the correct return type for the get() method on java.lang.ref.Reference.)
Maybe what we really want is a rule about picking the "most nullable" of the nullnesses that are "in play." The type parameter bound says "unspecified," but the instantiation says "nullable," so we go with nullable.
However, such a rule makes sense only for unspecified nullness, not in general: If the type parameter bound says "nullable" but the instantiation says "not null," then we still want not null. We could have a special case for unspecified nullness (as arguably we already do), but it would feel nice for it to be less of a special case.
This brings me back to an idea I mentioned once before (in the last paragraph of this post): What if, when determining the nullness of a type-variable usage, we didn't look at the bound at all? Then, to make unspecified nullness work the way we want it to again, we treat defaults as also applying to type-variable usages -- but only in combination with the type parameter that the class is instantiated with (by picking the "most nullable").
(This better fits my mental model for parametric nullability in unannotated classes, and it also produces a better (IMO) result for incrementally annotating a type, as discussed in bullet 2 in a post I linked earlier.)
I'm still not sure of the right way to communicate all this. I feel like it wouldn't be significantly more complex than the existing proposal once it's written in definition form. But people would need an intuitive feel for it, too, and I'm not sure I'm getting the intuitive model across.
Activity
cpovirk commentedon Oct 17, 2019
Hmm, maybe this should be part of #45?
kevinb9n commentedon Oct 17, 2019
So far I agree: you probably do expect that, and you probably should get a warning, and the question of whether you will get what you expect or not after ignoring/suppressing that warning is probably unanswered. (I believe you should, because (a) look, you were warned, and (b) otherwise the rules for interacting with that reference are different depending on the foreign definition of
Reference, and we don't like that.)I believe this part of the spec is only talking about type usages of the type variable itself (which can only occur inside the scope of the
Referenceclass). When analyzing that class itself,Twould be seen as having unspecified nullness.Reference<@Nullable String>is a parameterization ofReferenceand has substituted away all occurrences ofT, so it's our defined substitution rules that will govern what nullness theget()return type has. As argued above I think@Nullableshould probably win.So let me pause there and ask you whether that changes the rest of what you want to say here.
cpovirk commentedon Oct 18, 2019
Thanks, that changes a lot. I will say something different, and I will say it on #45, which is definitely getting at the same thing.
[-]What happens to `@NullnessUnspecified T` when `T` is instantiated with `@Nullable Foo`?[/-][+]What happens to `@NullnessUnspecified T` when the class is instantiated / parameterized?[/+][-]What happens to `@NullnessUnspecified T` when the class is instantiated / parameterized?[/-][+]What happens to `@NullnessUnspecified T` when `T` is instantiated with `@Nullable Foo`?[/+]