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

Iterable.any and Future.any are inconsistent/confusing #31354

Open
jakemac53 opened this Issue Nov 10, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@jakemac53
Contributor

jakemac53 commented Nov 10, 2017

See this example:

import 'dart:async';

main() async {
  var syncValues = [1, 2, 3, 4];
  print(syncValues.any((v) => v == 2)); // prints `true`
  
  var asyncValues = syncValues.map((v)=> new Future.value(v));
  print(await Future.any(asyncValues.map((v) async => await v == 2))); // prints `false`
}

The second one prints false because it returns the first completed future, which is the result of 1 == 2.

I recently ran into this when converting some sync code to async code, I saw that Future.any was a thing and assumed it worked the same. It should be easy and consistent to convert between async and sync code.

I understand changing the name or behavior here would be a large breaking change, so it may not be possible, but could possibly be considered for Dart 2.x, or 3.x etc? Another option could be deprecating Future.any and renaming it to Future.firstCompleted or something along those lines. Then users would see a deprecation notice pointing them to the new name which is more clear about what it does, and it wouldn't break existing users.

@jakemac53

This comment has been minimized.

Show comment
Hide comment
@jakemac53

jakemac53 Nov 10, 2017

Contributor

Fwiw, this method does not appear to be well known or often used, only 100 results in all public github repositories, including false positives https://github.com/search?utf8=%E2%9C%93&q=%22Future.any%22+extension%3A.dart&type=Code.

Contributor

jakemac53 commented Nov 10, 2017

Fwiw, this method does not appear to be well known or often used, only 100 results in all public github repositories, including false positives https://github.com/search?utf8=%E2%9C%93&q=%22Future.any%22+extension%3A.dart&type=Code.

@floitschG

This comment has been minimized.

Show comment
Hide comment
@floitschG

floitschG Nov 22, 2017

Contributor

Agreed.
The Future.any function should have a different name.

Contributor

floitschG commented Nov 22, 2017

Agreed.
The Future.any function should have a different name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment