You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Legality of type arguments in constructor invocations #58
This issue speaks of checking expressions, so it shouldn't affect our spec, but I believe it is important to think about the questions it poses
In #57 we considered well-formedness rules for type instantiations and came to the conclusion that any instantiation of any generic class is OK from our standpoint. I think that in the case of constructor invocations we may want different rules.
Assuming C is declared as follows:
class C<T extends @NotNull Object> {
T get();
void set(T t);
}
(which in our current spec means that every occurrence of T in the body of C is implicitly annotated as @NotNull). Let's consider the following constructor invocations:
new C<@NotNull String>() - this is obviously OK
new C<@Nullable String>() - I'd argue that this is misleading, so it should be rejected with an error
new C<@UnknownNullness String>()- I'd say this should be allowed, but probably only in the lenient mode
new C<P>() where P is a parametric usage of a type parameter P extends @Nullable Object - I'd say this should be allowed even in strict mode, to avoid forcing users to inject pointless @NotNull
I think I would want the same well-formedness rules both for type instantiations and for constructor invocations: #57 (comment)
Regarding usage of a type variable as a type parameter: I'm split on this issue.
On the one hand I can see how handling it similarly to wildcards and silently using the intersection of the bounds would reduce the annotation effort.
On the other hand it is as misleading as using C<@Nullable String> - the type variable P could be instantiated as @Nullable String and I would expect a warning on C<P> and require C<@NotNull P>.
new C<P>() where P is a parametric usage of a type parameter P extends @Nullable Object - I'd say this should be allowed even in strict mode, to avoid forcing users to inject pointless @NotNull
Sorry, I may be missing context, but: Can we ask the same about types in signatures (which would affect our spec)?
If so, this is more to discuss in my favorite issue, #4 :) (Possibly it's more to discuss in #4 in either case, but if we can convince ourselves that the issue here comes up only when checking expressions, then I could try to twist that into an argument that @NotNull could be a per-checker extension rather than a core annotation.)
(I'm still a little doubtful that this case comes up much in practice, but I'll ask about that on #4.)
Regarding unwanted verbosity in constructor invocations let me just throw in that the standard Java way to avoid this is the diamond operator - provided we have a suitable target type.
Activity
wmdietlGC commentedon Aug 3, 2019
I think I would want the same well-formedness rules both for type instantiations and for constructor invocations: #57 (comment)
Regarding usage of a type variable as a type parameter: I'm split on this issue.
On the one hand I can see how handling it similarly to wildcards and silently using the intersection of the bounds would reduce the annotation effort.
On the other hand it is as misleading as using
C<@Nullable String>- the type variablePcould be instantiated as@Nullable Stringand I would expect a warning onC<P>and requireC<@NotNull P>.cpovirk commentedon Aug 5, 2019
Sorry, I may be missing context, but: Can we ask the same about types in signatures (which would affect our spec)?
If so, this is more to discuss in my favorite issue, #4 :) (Possibly it's more to discuss in #4 in either case, but if we can convince ourselves that the issue here comes up only when checking expressions, then I could try to twist that into an argument that
@NotNullcould be a per-checker extension rather than a core annotation.)(I'm still a little doubtful that this case comes up much in practice, but I'll ask about that on #4.)
stephan-herrmann commentedon Aug 6, 2019
Regarding unwanted verbosity in constructor invocations let me just throw in that the standard Java way to avoid this is the diamond operator - provided we have a suitable target type.
kevinb9n commentedon Jun 19, 2021
I think this sorted itself out.