-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Inference error with ternary operator and null safety #45941
Comments
This is behaving as designed. What's happening is that when compiling the subexpression
As you can see from the class hierarchy, either Before null safety, trying to assign an expression of type Because of that (and other reasons specific to null safety) we drastically limited implicit downcasts with the introduction of null safety: now we only do implicit downcasting if the expression in question has a static type of final PSW result = (bar ? CNB() : AB()) as PSW; |
I filed dart-lang/language#1618 with an idea for a future improvement of the least upper bound algorithm that would avoid this problem. |
Interesting! Thanks for the in-depth explanation! I am going to close this issue then since dart-lang/language#1618 sounds more actionable. |
@goderbauer, it might be helpful to note that it is also possible to give a bit of guidance to the least upper bound algorithm by upcasting one of the branches of the conditional expression such that the superinterface graph doesn't have that ambiguity: final result = bar ? CNB() as PSW : AB(); // OK, LUB of `PSW` and `AB` is `PSW`. We could include the declared type of In any case, we might prefer this approach because it is statically safe, and faster (there is no downcast). |
I would expect the following code to be valid and error free. However, if null safety is enabled, it gives the error mentioned in the comment below. (Without null safety, no error is reported.)
This came up in flutter/flutter#81969.
The text was updated successfully, but these errors were encountered: