Skip to content

Legality of type arguments in constructor invocations #58

Closed

Description

@abreslav

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

Activity

  1. wmdietlGC commented on Aug 3, 2019

    @wmdietlGC
    Collaborator

    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>.

  2. cpovirk commented on Aug 5, 2019

    @cpovirk
    Collaborator
    • 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)?

    class User<P extends @Nullable Object> {
        Optional<P> get();
    }

    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.)

  3. stephan-herrmann commented on Aug 6, 2019

    @stephan-herrmann

    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.

  4. kevinb9n commented on Jun 19, 2021

    @kevinb9n
    Collaborator

    I think this sorted itself out.

  5. added
    design
    on Mar 16, 2023
  6. added
    nullness
    on Mar 13, 2024
  7. added this to the 0.3 milestone on Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    designnullness

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions