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
In the case above, the compiler don't know that A<String> and B<int> are both Base<Object?> or Base<dynamic> and thus the ternary expression will be demoted to Object, giving a compilation error on both test1 and test2.
A value of type 'Object' can't be returned from the function 'test' because it has a return type of 'Base<Object?>'.
Using a explicitly cast will work:
// In this case, however, the linter will complain that the cast is unnecessary.Base<Object?> test1(int n) {
return n ==1?A<String>() asBase<Object?> :B<int>() asBase<Object?>;
}
The computed type here is the same before null-safety and after null-safety - Object in both cases. Pre-null-safety, the system just inserted an implicit cast for you, but in the null-safety release we disallowed implicit downcasts, and so you now get an error.
It would be nice to do a better job with the upper bound computation, but the nature of the Dart type system makes upper bounds extremely challenging, and so we often fall into less than ideal solutions.
For conditional expressions specifically, I would like us to stop using upper bounds when we don't have to, which would solve your example. See my comment on this issue for details.
I'll close this issue in favor of #1618 , feel free to follow up there.
I don't know if this is expected or is a bug.
Consider the following:
In the case above, the compiler don't know that
A<String>
andB<int>
are bothBase<Object?>
orBase<dynamic>
and thus the ternary expression will be demoted toObject
, giving a compilation error on bothtest1
andtest2
.Using a explicitly cast will work:
This worked before null-safety.
The text was updated successfully, but these errors were encountered: