Skip to content
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

Covariant return types for nullable defaults #48205

Closed
giladwo opened this issue Jan 24, 2022 · 7 comments
Closed

Covariant return types for nullable defaults #48205

giladwo opened this issue Jan 24, 2022 · 7 comments
Labels
type-question A question about expected behavior or functionality

Comments

@giladwo
Copy link

giladwo commented Jan 24, 2022

Functions like E firstWhere(bool test(E element), {E orElse()?}) have an invariant E return type.

Considering the fact that E extends E?, and orElse can usefully return null, I suspect a covariant contravariant T super E may be a more useful return type. (I'm not sure how to express this in Dart though).

What do you think?

@mraleph
Copy link
Member

mraleph commented Jan 25, 2022

You can't express this in Dart.

See related discussions at #42947

@mraleph mraleph closed this as completed Jan 25, 2022
@mraleph mraleph added the type-question A question about expected behavior or functionality label Jan 25, 2022
@giladwo
Copy link
Author

giladwo commented Jan 25, 2022

Sorry, I got mixed up and thought about this kind of variance:
https://github.com/Team1690/Scouting-Frontend/blob/master/lib/views/mobile/selector.dart#L24-L34
Could you please explain why wouldn't a T extends E work here?

@mraleph
Copy link
Member

mraleph commented Jan 25, 2022

Because T extends E allows for a more specific type and not for a less specific type.

You, on the other hand, would like to allow T to be E? if E is non-nullable - which is less specific. (E extends E? is satisfied, but E? extends E is not).

Unfortunately Dart does not support lower type bounds on type parameters, only upper type bounds, while languages like Java and Scala support both lower and upper type bounds.

@giladwo
Copy link
Author

giladwo commented Jan 25, 2022

So how come the above validate works as if E? extends E?

@mraleph
Copy link
Member

mraleph commented Jan 25, 2022

I am not sure I understand the question. Could you clarify which part of the code works as if E? extends E?

@giladwo
Copy link
Author

giladwo commented Jan 25, 2022

I've understood the mistake while trying to explain it here.
Thanks for the helpful clarification 😄

@eernstg
Copy link
Member

eernstg commented Jan 25, 2022

Note the overlap with dart-lang/language#1674.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

3 participants