Skip to content

StreamTransformer is difficult to use in Dart 2.0 #29878

@vsmenon

Description

@vsmenon

This has come up several times, but I don't see a bug for it.

Here's a sample use that highlights the problem:

import 'dart:async';

Stream<int> change(Stream<Iterable> stream) {
  return stream.transform(new StreamTransformer<Iterable, int>.fromHandlers(
      handleData: (Iterable iter, EventSink<int> sink) {
    sink.add(iter.length);
  }));
}

void main() {
  Stream<Iterable> stream = new Stream<List<String>>.fromIterable([
    ["hello"],
    ["world"]
  ]);
  var result = change(stream);
  result.listen(print);
}

The change method looks just fine from a typing perspective. However, if we invoke it with Stream<T> where T is a proper subtype of Iterable, it will trigger a confusing runtime error:

Type '_StreamHandlerTransformer<Iterable, int>' is not a subtype of type 'StreamTransformer<List<String>, int>'

Intuitively, StreamTransformer should be contravariant on it's first type parameter (Iterable here). But, Dart does not support that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.core-nenhancement-breaking-changeAn enhancement which is breaking.library-asynctype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions