Skip to content

syntax support for Stream programming #7002

@DartBot

Description

@DartBot

This issue was originally filed by @seaneagan


In the same way that programming with Futures could benefit from language support (issue #104), programming with Streams could as well. Streams are essentially async Iterables, so an async version of the "for" statement could work for Streams. It probably makes sense to call it "on" which is already a contextual keyword elsewhere. As an example the implementation of the Iterable-based methods on Stream could look identical to how they are in Iterable except replacing "on" with "for":

async bool any(bool f(E e)) {
  on(var e in this) {
    if(f(e)) return true;
  }
  return false;
}

notes:

* the "on" statement would trigger a "subscribe" to the Stream passing onData, onDone, and onError resulting in a StreamSubscription

  • as with await, the enclosing function would implicitly return a Future
  • as with await, errors within the construct would throw the unwrapped error, not the AsyncError wrapper
  • any "break" or "return" in the "on" statement would trigger an "unsubscribe" on the StreamSubscription
  • nested "await"s and "on"s could be delimited by "pause()" and "resume()" on the StreamSubscription, and since that's only a best effort, any events which occur before the nested items complete could be buffered for use in later iterations of the "on" statement.

Initial mailing list discussion:

http://goo.gl/8PqcP

Activity

anders-sandholm

anders-sandholm commented on Nov 29, 2012

@anders-sandholm
Contributor

cc @floitschG.
Added Area-Language, Triaged labels.

DartBot

DartBot commented on Jan 2, 2013

@DartBot
Author

This comment was originally written by jjinux...@google.com


Thank you for the excellent issue description!

DartBot

DartBot commented on Feb 5, 2014

@DartBot
Author

This comment was originally written by @seaneagan


Related idea... async generators (issue #36 is sync generators). Functions which use (await OR on (see above)) AND yield would return Streams.

Examples:

  1. An easier way to do StreamTransformers:

Stream everyOtherEvent(Stream stream) {
  int i = 0;
  on(var e in Stream) if(i.isEven) yield e;
}

  1. Flexible Stream generation:

/// Think... "Connection error... retrying in <x seconds>"
Stream<Duration> retry(action, Duration initial, {int max}) {
  var duration = initial;
  var tries = 0;
  while(max == null || tries++ <= max) {
    if(action()) return;
    yield await new Future.delayed(duration);
    duration *= 2;
  }
  throw '...';
}

gbracha

gbracha commented on Aug 22, 2014

@gbracha
Contributor

We are looking at adding such support as part of the async-await proposal.


Set owner to @gbracha.
Added Accepted label.

gbracha

gbracha commented on Jan 3, 2015

@gbracha
Contributor

The Dart spec now includes async* methods and await for loops. Not fully implemented yet, but in progress. I'm closing this bug as "Done" from the language perspective.


Added Done label.

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

Metadata

Metadata

Assignees

Labels

area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @anders-sandholm@gbracha@DartBot@mit-mit

      Issue actions

        syntax support for Stream programming · Issue #7002 · dart-lang/sdk