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

Extension type implementing Stream can't be used as a return types of a async* function #55285

Closed
Cat-sushi opened this issue Mar 23, 2024 · 3 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report

Comments

@Cat-sushi
Copy link

extension type CsvStream(Stream<List<String?>> _)
    implements Stream<List<String?>> {}

CsvStream readCsvLines(String filepath) async* {
///
}
Functions marked 'async*' must have a return type that is a supertype of 'Stream<T>' for some type 'T'.
Try fixing the return type of the function, or removing the modifier 'async*' from the function body.dart[illegal_async_generator_return_type](https://dart.dev/diagnostics/illegal_async_generator_return_type)

Dart SDK version: 3.3.1 (stable) (Wed Mar 6 13:09:19 2024 +0000) on "linux_x64"

@Cat-sushi
Copy link
Author

Following example is fine,

extension type Csv(List<List<String?>> _) implements List<List<String?>> {}

Csv parseCsvLines(String string) {
///
}

@eernstg
Copy link
Member

eernstg commented Mar 25, 2024

An async* function will return an instance of Stream<T> for some T, no matter what, so your declared return type can't promise a proper subtype of that (you can't expect that function to return a MySpecialStream<OfAnything>, because the code that creates the object which is returned isn't written by you, it's generated by the compiler).

However, it would actually be sound to allow this return type to be an extension type whose representation type is an acceptable return type for the async* function: If it has representation type Stream<int> and we're actually returning a Stream<int> then it doesn't hurt (from a soundness perspective) that it is typed as a MyExtensionType, where

extension type MyExtensionType(Stream<int> it) implements Stream<int> {...}

See dart-lang/language#3607 for a proposal to do this. You can vote for this proposal if you wish to support the introduction of this kind of relaxation of the rules about the return type of async* and similar functions.

@lrhn
Copy link
Member

lrhn commented Mar 25, 2024

Which is also to say that this is working as currently intended, and it's a language change to make it work differently.
The issue belongs in the language repo, not here, but drive there is already an issue there, I'll just close this as a duplicate.

@lrhn lrhn added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report labels Mar 25, 2024
@lrhn lrhn closed this as not planned Won't fix, can't repro, duplicate, stale Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report
Projects
None yet
Development

No branches or pull requests

3 participants