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
Propagation of nullability info from overridden methods in supertypes #14
If there's an unannotated method having an annotated overridden method in a supertype, should we treat the former one as having the same nullability annotations as the latter? I.e, propagating the nullability info
In Kotlin, we already do this for supported annotations
changed the title [-]Propagation of nullability info from overridden[/-][+]Propagation of nullability info from overridden methods in supertypes[/+]on Dec 3, 2018
Class Super is either explicitly annotated or comes from a nullable default context.
Class Sub is in a not-null-by-default context.
The reference implementation should issue an error for the invalid parameter type in the override and for violations of behavioral subtyping in general. (That is, parameters can only get wider, returns only narrower.)
Note that specializing the return type to be NotNull would be sound. The following would work ok:
Making the Nullable annotation on the parameter explicit simplifies reading code.
That is, there are only two places to look at to determine an effective type:
Explicit annotations
A NotNullDefault annotation on the enclosing top-level type or directly enclosing package
I generally agree with the decision made here. Just want to let you know that some Eclipse users were very persistent in requesting an option to propagate nullness down from super signatures. Maybe s.t. that could be reconsidered as an option after a 1.0 Release.
Today, as long as you understand whether you're in a null-marked context or not, you can read code and know what the nullness of every type usage is. At most, you may have to scroll up to where a variable, type variable, etc. was declared to know it.
Any kind of automatic propagation/inheritance defeats that. If we did that even in a few odd cases, users would lose their ability to trust their own eyes, and nullness semantics would become mysterious.
Put simply: "String means String." Full stop. This is a core value of the project.
Here's another, more minor (but still real) issue: suppose we have, in null-marked context:
interface Foo<T extends @Nullable Object> {
@Nullable T method(@NonNull T t);
}
interface Bar<T extends @Nullable Object> extends Foo<T> {
T method(T t);
}
If either of the nullness annotations were automatically inherited from Foo.method to Bar.method ... how would Bar.method express that it doesn't want that inheritance in this case - that it wants T to be parametric? It's perfectly valid contractually (it is expanding the set of what it accepts, and refining the type of what it returns).
Activity
[-]Propagation of nullability info from overridden[/-][+]Propagation of nullability info from overridden methods in supertypes[/+]wmdietlGC commentedon Jan 10, 2019
We discussed this issue some more and preferred explicit annotations.
Class Super is either explicitly annotated or comes from a nullable default context.
Class Sub is in a not-null-by-default context.
The reference implementation should issue an error for the invalid parameter type in the override and for violations of behavioral subtyping in general. (That is, parameters can only get wider, returns only narrower.)
Note that specializing the return type to be NotNull would be sound. The following would work ok:
Making the Nullable annotation on the parameter explicit simplifies reading code.
That is, there are only two places to look at to determine an effective type:
kevinb9n commentedon Jun 17, 2019
Agreed, no inheriting nullability annotations.
stephan-herrmann commentedon Jul 27, 2019
I generally agree with the decision made here. Just want to let you know that some Eclipse users were very persistent in requesting an option to propagate nullness down from super signatures. Maybe s.t. that could be reconsidered as an option after a 1.0 Release.
kevinb9n commentedon Aug 12, 2024
Today, as long as you understand whether you're in a null-marked context or not, you can read code and know what the nullness of every type usage is. At most, you may have to scroll up to where a variable, type variable, etc. was declared to know it.
Any kind of automatic propagation/inheritance defeats that. If we did that even in a few odd cases, users would lose their ability to trust their own eyes, and nullness semantics would become mysterious.
Put simply: "
StringmeansString." Full stop. This is a core value of the project.Here's another, more minor (but still real) issue: suppose we have, in null-marked context:
If either of the nullness annotations were automatically inherited from
Foo.methodtoBar.method... how wouldBar.methodexpress that it doesn't want that inheritance in this case - that it wantsTto be parametric? It's perfectly valid contractually (it is expanding the set of what it accepts, and refining the type of what it returns).