-
Notifications
You must be signed in to change notification settings - Fork 205
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
??=
fails to promote variable whose type is a nullable type parameter
#3057
Comments
There was a substantial amount of discussion about a similar situation here: #3031. The two situations differ in that the expression The compound assignment class C<T> {
T f(T? t, T u) {
t == null ? t = u : t;
return t;
}
}
main() {} In the rewrite we still don't get the promotion, in spite of the fact that Hence, it's quite surprising that this version of the code also doesn't promote Here is another variant where class C<T> {
T f(T? t, T u) {
if (t == null) t = u;
return t;
}
}
main() {} Is there something fishy about types of the form |
The promotion of So (I decided after checking that it wasn't my first guess) it's probably about unifying If I add class C<T> {
T f(T? t, T u) {
t as T;
if (t == null) t = u;
return t;
}
}
main() {} |
I dug into the implementation and confirmed that this is indeed what's happening. I think we should consider, in future versions of Dart, extending the notion of "type of interest" so that for a variable of type Since this would require a language change (which theoretically could break existing code), I'll move this issue to the language repo for further discussion. |
Indeed. We already say that if Which takes us from We don't cover potentially nullable, which is what Or will probably just be a slight tweak to finding related types of interest when the type is a type variable, or intersection type of one. Seems doable, and not too dangerous. Maybe we could say that if a union type is of interest, so is each part type. Then I'm not sure it helps here, unless we can demote to a related type of interest. |
The following code has a compile-time error at (2) ("A value of type 'T?' can't be returned from a function with return type 'T' because 'T?' is nullable and 'T' isn't."):
It seems like the null-aware assignment at (1) should promote the type of
t
fromT?
toT
, but apparently this isn't happening.The text was updated successfully, but these errors were encountered: