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
Nullability of a type based on generic parameter depending on the specific argument #13
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
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
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.
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.
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
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.
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...]
Activity
[-]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[/+]wmdietlGC commentedon Jan 10, 2019
By default type parameters should depend on both the type annotations and the Java base type.
Let's expand the example:
That way, type parameters naturally behave over the qualified types. Exceptions can be made using explicit annotations on type parameter uses.
abreslav commentedon Jun 18, 2019
I think this is resolved now:
Boxclass is in the legacy mode, thenget()returns a legacy type (something likeString!in Kotlin)@DefaultNotNullor@DefaultNullablemode thenTinT get();is parametric, soBox<@XX String>.get()returns@XX Stringkevinb9n commentedon Jun 26, 2019
Our current treatment is to say that a type variable usage is undefaultable (does not respect defaults). Instead, its nullness is:
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
All the preceding is rough and has issues.
kevinb9n commentedon Jun 26, 2019
What parameterizations are allowed?
dzharkov commentedon Jul 15, 2019
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
kevinb9n commentedon Jul 15, 2019
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.
dzharkov commentedon Jul 16, 2019
I see, thanks
cpovirk commentedon Oct 30, 2019
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...]
kevinb9n commentedon May 20, 2020
This is all addressed by more specific issues now.