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

await for (final _ in stream) for Stream<void> results in build error #48347

Closed
Atokulus opened this issue Feb 8, 2022 · 2 comments
Closed
Assignees
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@Atokulus
Copy link

Atokulus commented Feb 8, 2022

Dear Dart/Flutter community

I am currently trying to process a PriorityQueue<Item> (https://api.flutter.dev/flutter/package-collection_collection/PriorityQueue-class.html) asynchronously. In my implementation I first add a new workload item to the queue and an event is emitted to the stream of a StreamController<void> to signal new queue item(s) are available.

An asynchronous await for loop receives the event from the stream, then checks the queue and processes all its items one after another as a whole, before receiving and processing the next stream event.

As a matter of convenience, I tried the following:

await for (final _ in _eventStreamController.stream) {
  while (_queue.isNotEmpty) {
    final item = _queue.removeFirst();
    await item.process();
  }
}

This code will result in a build error: Error: This expression has type 'void' and can't be used.

It would be great to allow for such a pattern for void events. I am currently using a Stream<Null> to circumvent the build error (which is advised against by the linter).

The use of .listen() does not seem to be an option: Each workload item would be immediately processed when emitted, without any prioritizing taking place anymore, and the added risk of race conditions (I am currently work with Bluetooth Low Energy communications).

I hope this report finds you well and am looking forward to your response😁

Best regards
Markus

@jakemac53
Copy link
Contributor

It looks like the Analyzer does not report an error here, while the CFE does, so one of them probably has a bug.

For your particular use case it should be sufficient to use listen, but track whether you are currently draining the queue with a variable. If you are already draining it just return from the handler. That might be a slightly more ideal way of handling this anyways (more eagerly drains the "signal" stream).

@lrhn
Copy link
Member

lrhn commented Feb 8, 2022

That is a bug, there is nothing in the code here which warrants the error that CFE gives.

Error: This expression has type 'void' and can't be used.
await for (final _ in _eventStreamController.stream) {
                 ^

It seems the await for is being desugared/transformed into something that uses the void typed event value, even though the original code never uses it for anything other than assigning to _.

@lrhn lrhn transferred this issue from dart-lang/language Feb 8, 2022
@lrhn lrhn added area-front-end Use area-front-end for front end / CFE / kernel format related issues. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Feb 8, 2022
@johnniwinther johnniwinther self-assigned this Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants