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

Explicitly type Stream argument as List<int> #56

Merged
merged 2 commits into from Jul 6, 2023
Merged

Conversation

mkustermann
Copy link
Contributor

@mkustermann mkustermann commented Jul 6, 2023

A breaking change (see [0]) will make utf8.encode() return the more precise Uint8List type (instead of the current List<int>).

In rare circumstances this can lead to changes in behavior, mainly when code relies on type inference, a different type got inferred and the code dependend on the type not being inferred a more precise type.

Here we explicitly use Stream<List<int>> instead of relying on type inference (which would infer Stream<Uint8List> in some cases after [0]). This is necessary as the stream transformer APIs cannot work with subtypes. Example of code that fails at runtime:

  import 'dart:typed_data';
  import 'dart:convert';

  void main() {
    Stream<Uint8List> stream = Stream.fromIterable([]);
    Stream<List<int>> stream2 = stream;
    stream2.transform(utf8.decoder);
         //  ^^^ Will throw due to Utf8Decoder not being subtype of
         //      StreamTransformer<Uint8List, String>.
  }

[0] dart-lang/sdk#52801

mkustermann and others added 2 commits July 6, 2023 14:09
A breaking change (see [0]) will make `utf8.encode()` return the more precise
`Uint8List` type (instead of the current `List<int>`).

In rare circumstances this can lead to changes in behavior, mainly when
code relies on type inference, a different type got inferred and the code
dependend on the type not being inferred a more precise type.

Here we explicitly use `Stream<List<int>>` instead of relying on type
inference (which would infer `Stream<Uint8List>` in some cases after
[0]). This is necessary as the stream transformer APIs cannot work with
subtypes. Example of code that fails at runtime:

```
  import 'dart:typed_data';
  import 'dart:convert';

  void main() {
    Stream<Uint8List> stream = Stream.fromIterable([]);
    Stream<List<int>> stream2 = stream;
    stream2.transform(utf8.decoder);
         //  ^^^ Will throw due to Utf8Decoder not being subtype of
         //      StreamTransformer<Uint8List, String>.
  }
```

[0] dart-lang/sdk#52801
@nex3 nex3 merged commit 36cb86d into google:main Jul 6, 2023
7 checks passed
@mkustermann
Copy link
Contributor Author

Thank you for the quick review & publish of package, @nex3 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants