Skip to content

Nullability of a type based on generic parameter depending on the specific argument #13

Closed

Description

@dzharkov

Consider the following class:

interface Box<T> {
    T get();
    void set(T t);
}

Having a type Box<@Nullable String> or Box<String?> in Kotlin, what's the return type of Box.get then?
We had an idea to have a special annotation @StrictNullness for type parameters
that makes nullability of types based on them depend on nullability of the specified type argument

Activity

  1. changed the title [-]Nullability of a type based on generic parameter depending the specific argument[/-] [+]Nullability of a type based on generic parameter depending on the specific argument[/+] on Dec 3, 2018
  2. wmdietlGC commented on Jan 10, 2019

    @wmdietlGC
    Collaborator

    By default type parameters should depend on both the type annotations and the Java base type.
    Let's expand the example:

    interface Box<T extends @Nullable Object> {
        T get();
        @Nullable T getNbl();
        @NotNull T getNotNull();
    }
    
    Box<@NotNull String> bs;
    bs.get() has type @NotNull String
    bs.getNbl() has type @Nullable String
    bs.getNotNull() has type @NotNull String
    
    Box<@Nullable String> bns;
    bns.get() has type @Nullable String
    bns.getNbl() has type @Nullable String
    bns.getNotNull() has type @NotNull String

    That way, type parameters naturally behave over the qualified types. Exceptions can be made using explicit annotations on type parameter uses.

  3. added
    nullness
    on Jan 10, 2019
  4. abreslav commented on Jun 18, 2019

    @abreslav

    I think this is resolved now:

    • if the Box class is in the legacy mode, then get() returns a legacy type (something like String! in Kotlin)
    • if it's in the @DefaultNotNull or @DefaultNullable mode then T in T get(); is parametric, so Box<@XX String>.get() returns @XX String
  5. kevinb9n commented on Jun 26, 2019

    @kevinb9n
    Collaborator

    Our current treatment is to say that a type variable usage is undefaultable (does not respect defaults). Instead, its nullness is:

    • not-null, if the type parameter bound is not-null (whether via default or not)
    • unknown, if the type parameter bound is "unknown" (whether via default or not)
    • but if the bound is nullable, then the nullness is "parametric". This produces a situation similar to @NullnessUnknown, except would remain strictly enforced even for the (most) users who want unknown conversions to be leniently enforced.

    This refers to usages of the type variable itself inside its scope. As for what the consumer of this API sees after substitution, for a parameterization that doesn't specify any annotations of its own, that would be

    • if the type variable usage was not-null or nullness-unknown, it is the same after substitution.
    • if the type variable usage was nullable.... this is a case where it may make sense to respect the default nullness that is in effect at the source location where the parameterized type appears.

    All the preceding is rough and has issues.

  6. kevinb9n commented on Jun 26, 2019

    @kevinb9n
    Collaborator

    What parameterizations are allowed?

    • If the type parameter is not-null, specifying an argument that is annotated nullable or unknown should be an error (potentially suppressable; if so, the argument annotation should override the bound annotation as if it was not there, which one can imagine will lead to bugs).
    • If the type parameter is nullness-unknown, specifying an argument with known nullness should produce only the weaker type of warning that most users will suppress.
    • If the type parameter is nullable, any annotation works on the argument.
  7. dzharkov commented on Jul 15, 2019

    @dzharkov
    CollaboratorAuthor

    What if the type usage bases on type variable with nullable/unknown upper bound is annotated as @NotNull?
    It might mean that this type usage is not nullable independently of the substitution

  8. kevinb9n commented on Jul 15, 2019

    @kevinb9n
    Collaborator

    I believe our plan is that any type variable usage can always specify an explicit nullness annotation which takes precedence over what came from the bounds.

  9. dzharkov commented on Jul 16, 2019

    @dzharkov
    CollaboratorAuthor

    I see, thanks

  10. cpovirk commented on Oct 30, 2019

    @cpovirk
    Collaborator

    In #45, I've been proposing not to give precedence to explicit annotations over the substituted argument. Instead, I'm proposing for us to consider both values, picking the "more nullable" of the two.

    In practice, the two policies usually produce the same results -- especially if we omit @NotNull (#4) and @DefaultNullable (#41) :)

    I have said lots more about this elsewhere. I'm going to try to copy or rewrite portions of it on this issue, where it actually belongs.

    [edit: or maybe not; still trying to figure out how to attack this...]

  11. kevinb9n commented on May 20, 2020

    @kevinb9n
    Collaborator

    This is all addressed by more specific issues now.

  12. added
    design
    on Mar 13, 2024
  13. 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