Skip to content

Possible uses for annotating an unbounded wildcard: Foo<@NullnessAnnotation ?> #88

Closed

Description

@kevinb9n

Schemes where a wildcard might be annotated separately from its bound are discussed in #31, and I think we're in some stage of ruling those out. In this issue I want to consider whether we should support limited cases of annotating a wildcard, only when there is no provided bound.

Assume we say "no" to #85 (because I assume if we said yes it will dictate what we must do here).

Suppose we have class Foo<T extends @Nullable Base>.

  • Foo<@NonNull ?> could be a shorthand for Foo<? extends @NonNull Object> or Foo<? extends @NonNull Base>. It's now a reifiable type (is that good?), it's shorter, and it doesn't involve either redundantly writing Base (latter) OR strangely implying that the wildcard is bounded by Object when it really isn't (former). It could be thought of as a "type projection" of a wildcard (Non-null projection of a type variable usage #86) and that seems fairly okay.

  • Foo<@Nullable ?> could do the analogous thing, but here there is actually a big problem: it fails to emphasize the "might be nullable, might be non-null" meaning and will be taken erroneously by many users as being definitely-nullable. But if we decide Nullness bound for unbounded wildcard in a nullness-annotated context #83 in the way we're starting to lean (the ? in Foo<?> is implicitly nullable) then there won't be a reason to write this anyway.

To me, reasonable options seem to include allowing neither form, or allowing only the first one.

Activity

  1. added
    nullness
    on Dec 12, 2019
  2. kevinb9n commented on Dec 12, 2019

    @kevinb9n
    CollaboratorAuthor

    With Foo<@NonNull ?> being, as far as the compiler is concerned, reifiable, we should be able to write an array type Foo<@NonNull ?>[] warning-free. But see https://bugs.openjdk.java.net/browse/JDK-8233945

  3. kevinb9n commented on Dec 18, 2019

    @kevinb9n
    CollaboratorAuthor

    Is this a case where we can safely go with "allow neither" for now, and we're still free to consider adding it on in the future?

  4. kevinb9n commented on Jan 10, 2020

    @kevinb9n
    CollaboratorAuthor

    I'll answer my own question then: yes, it is. Current plan should be to not do this, but we have potential to offer a shorthand in the future.

  5. added this to the 0.3 milestone on Dec 2, 2022
  6. added
    design
    on Mar 13, 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