-
Notifications
You must be signed in to change notification settings - Fork 213
Ternary conditional operator does not resolve type properly #2577
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
Comments
This example is different from others in that it does not even require generics 😢 . The issue here comes from the combination of |
The cause of the issue here is that In case of such an ambiguity, the least-upper-bound algorithm gives up on those types, and here it then finds The real issue is that we dont use the context type information to help disambiguate. We have available information that could help us, we just don't use it. |
This is also a problem (unsurprisingly) with switch expressions: Is there any potential for fixing this? eg bias towards the type declared in the function signature (or variable declaration, if applicable)? While this is pretty niche, as far as I can tell there's no way to write this logic in a compile-time safe manner so it seems like a pretty unfortunate limitation. |
Yes, definitely. Switch expressions have made least upper bound a bigger pain point than it already was.
Yes, I think we're leaning towards using the context type as a hint to least upper bound. The tracking issue for that is #1618. I'll close this one in favor of that.
You can always just upcast. Since it's an upcast, it will never fail. Having to upcast is annoying though, since it's hard to tell if a given cast is casting up (safe) or down (may fail at runtime). |
This yields the error
A value of type "Object" can't be returned from the function 'func' because it has a return type of 'B'.
. This error does not occur using regular conditional syntax (if/else). I am using Dart 2.18.2, and this can be replicated on DartPad.The text was updated successfully, but these errors were encountered: