-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do we need a generic constraint for nullable only generics? #143
Comments
For the record, my position on this is and has been that I don't think this is needed or very useful. If we get evidence to the contrary, I'm definitely open to adding it. When I was prototyping NNBD a while back and converting over the core libraries, I don't recall running into cases where I wanted this. |
@lrhn has a concern around ending up with classes that are equivalent for nullable and non-nullable type, but end up with incompatible types. For example: class A<T extends Object?> {
final T? value;
A(this.value);
} Now This can be avoided if we are able to write a constraint saying that the type variable must be non-nullable: class A<T extends Object> {
final T? value;
A(this.value);
} This class can only be instantiated as The alternative of having a constraint that a type variable must be nullable avoids this. // T is required to be nullable
class A<T? extends Object?> {
final T value;
A(this.value);
} Now you can never write |
@leafpetersen wrote
If that other type variable, Otherwise, if that other type variable |
@lrhn Where are you in your thinking on this? I still think that I'm on the side of the fence that this is not needed, but open to discussion. |
I think I'm also hoping it won't be needed, but I want to keep the door open to being convinced otherwise. If we start seeing classes where the type variable is nullable-bounded (or unbounded) and all occurrences of the type variable has a If we have |
I think we're converging towards holding off on this. @lrhn is there discussion/thoughts you want to raise or just record here for posterity? |
I've become more convinced that not having this feature is a good thing. If you actually care whether your the type of your type variable is nullable, you should just make it non-nullable and write the I'm actually close to requiring all actual bounds to be non-nullable, the only way to get non-nullable type variable is to use no bound and accept all types, because that means that you won't be able to expect anything from the object anyway, only passing it through uninspected. A generic type parameter like I think allowing nullable type bounds is fine from a consistency standpoint, but I expect style guides to recommend against it unless the bound is |
@leafpetersen, @lrhn, @munificent, sounds like we can conclude that we will not have a mechanism that allows a type variable declaration to require that the value must be a nullable type. "Decision" and close? |
Decided, no, we can always add this later if we find it's important. |
The following class will be ill-formed when we move to non-nullable types:
One solution is to simply require the user to manually put a
?
on the type where required:Another possibility is to allow the class to specify a constraint indicating that it may only be applied to nullable types:
It's not clear how common this is. Also, this is something that could probably be added later if we discover we really want it (though changing classes to use it would be a breaking library change).
cc @lrhn @munificent @eernstg
The text was updated successfully, but these errors were encountered: