dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.21k stars 1.57k forks source link

Add Stream.whereType like Iterable.whereType #34050

Open denniskaselow opened 6 years ago

denniskaselow commented 6 years ago

My use case are WebSockets where I have to do:

webSocket
    .where((data) => data is Uint8List)
    .cast<Uint8List>()
    .listen((data) {
  // ...
});

instead of

webSocket
    .whereType<Uint8List>()
    .listen((data) {
  // ...
});

Not that much of a difference but shouldn't it be there for the sake of consistency?

MarcelGarus commented 4 years ago

I'd really like this too. I currently have a Stream of Events of different subtypes and I'd like to filter for one event type in particular.

natebosch commented 4 years ago

This is available as an extension method from both stream_transform and rxdart.

julemand101 commented 1 year ago

Would it be worth consider this to be fixed as part of Dart 3.0? It is going to be a breaking change but a smaller one which I don't personally think would be too much to ask for since it is an annoying inconsistency between the current Iterable and Stream.

lrhn commented 1 year ago

Not planning on any new breaking changes. We're already well towards doing the ones we have planned.

Adding it as an extension method is more likely. We are considering whether to add more extensions methods, probably selected from package:async and package:collection, to the 3.0 platform libraries.

denniskaselow commented 1 year ago

With Iterable receiving nonNulls, firstOrNull, lastOrNull, singleOrNull, elementAtOrNull and indexed in 3.0 and Stream/package:async apparently receiving none of them, is this still being considered? If it is, I'd say it would be nice to also add the ones relevant to Stream (and maybe change the issue title to highlight the point of making the API of iterables and streams consistent/interchangeable).

If not, I'd say this issue can be closed though I still feel like the APIs should be the same where possible. But back when I first opened the issue there were no extension methods and adding a dependency that adds them or implementing them isn't that much of an effort.

EDIT: Or move the issue to https://github.com/dart-lang/async? I just saw that firstOrNull already exists in https://github.com/dart-lang/async/blob/master/lib/src/stream_extensions.dart

lrhn commented 1 year ago

Streams differ from iterables in that they have (at least) two different use-cases, as represented by single-subscription streams and broadcast streams. Filtering a single-subscription stream is dangerous, because it means the filtered-out events are completely lost. Filtering a broadcast stream is perfectly fine, someone else might handle the events instead.

This ambiguity of design means that the Stream type supporting both (and more, with Stream.multi) isn't always the best place to add functionality that only really applies to one of the use-cases. But it's the only type there is (there are no BroadcastStream and SingleSubscriptionStream subtypes), and we haven't been particularly picky about all the original methods. (Really, elementAt? That's just for hobgoblin-consistency with Iterable.)

I'd be fine with adding whereType as an extension in package:async or in dart:async (re-exported by dart:core since it already exports Stream), but it's not been a priority for Dart 3.0, because it's not linked to the new features, so it can be done at any time.