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

Proposal: Add AsyncEnumerable.Empty<T>() #1128

Closed
jeremyVignelles opened this issue Dec 22, 2019 · 9 comments
Closed

Proposal: Add AsyncEnumerable.Empty<T>() #1128

jeremyVignelles opened this issue Dec 22, 2019 · 9 comments
Labels
area-System.Linq untriaged New issue has not been triaged by the area owner

Comments

@jeremyVignelles
Copy link

Rationale

I have an interface which is written like this:

    public interface IItemRetriever
    {
        public IAsyncEnumerable<string> GetItemsAsync();
    }

I want to write an empty implementation that returns no item, like so:

    public class EmptyItemRetriever : IItemRetriever
    {
        public IAsyncEnumerable<string> GetItemsAsync()
        {
           // What do I put here if nothing is to be done?
        }
    }

If it was a plain IEnumerable, I would return Enumerable.Empty<string>();, but I didn't find any AsyncEnumerable.Empty<string>().

Proposal

Create an AsyncEnumerable static class that contains an Empty<T>() static method.

In that case, I would just write:

return AsyncEnumerable.Empty<string>();

Workarounds

I found this which works but is quite weird:

        public async IAsyncEnumerable<string> GetItemsAsync()
        {
            await Task.CompletedTask;
            yield break;
        }
@YairHalberstadt
Copy link
Contributor

This is a library feature so should be asked at dotnet/runtime

@YairHalberstadt
Copy link
Contributor

By the way, IAsyncEnumerable is just a normal interface. It should be extremely easy to implement AsyncEnumerable.Empty yourself.

@jeremyVignelles
Copy link
Author

Sorry, I was unsure about where to ask that. Can someone move the issue to the appropriate repo or do I need to close this to submit it to the repo?

Yes, I should be able to implement that myself, but something inside the framework would be really helpful.

@YairHalberstadt
Copy link
Contributor

It can be moved, but it's probably quicker to just do it yourself :-).

The System.Async.Linq nuget package has an Empty method.

@aalmada
Copy link

aalmada commented Dec 22, 2019

It's available in System.Linq.Async https://github.com/dotnet/reactive/blob/master/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Empty.cs

@jeremyVignelles
Copy link
Author

OK, so if you think that it doesn't make sense to add it to the runtime, this can be closed.

@yaakov-h
Copy link
Member

This wouldn't be the place to make that call anyway.

@aalmada
Copy link

aalmada commented Dec 22, 2019

This would be a runtime (previously corefx) issue.
Still, the logic up until now has been to define IEnumerable and IAsyncEnumerable in System.Collections and System.Collections.Generic. While Enumerable is defined in System.Linq and AsyncEnumerable is defined in System.Linq.Async. This last one is a NuGet package.

@333fred 333fred transferred this issue from dotnet/csharplang Dec 23, 2019
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Linq untriaged New issue has not been triaged by the area owner labels Dec 23, 2019
@stephentoub
Copy link
Member

We don't currently have any plans to add async LINQ equivalents to dotnet/runtime. As called out by previous commenters, the System.Linq.Async nuget package provides an extensive set of such helpers, including Empty. And as was pointed out in the original comments, you can write your own simple version of this:

#pragma warning disable CS1998
        private static async IAsyncEnumerable<T> Empty<T>()
#pragma warning restore CS1998
        {
            yield break;
        }

As such, I'm going to close the issue, but it can always be revisited in the future if it proves to be a significant pain point.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Linq untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

6 participants