8

I'd like to know if there's a way I can write a function to "pass through" an IAsyncEnumerable... that is, the function will call another IAsyncEnumerable function and yield all results without having to write a foreach to do it?

I find myself writing this code pattern a lot. Here's an example:

async IAsyncEnumerable<string> MyStringEnumerator();

async IAsyncEnumerable<string> MyFunction()
{
   // ...do some code...

   // Return all elements of the whole stream from the enumerator
   await foreach(var s in MyStringEnumerator())
   {
      yield return s;
   }
}

For whatever reason (due to layered design) my function MyFunction wants to call MyStringEnumerator but then just yield everything without intervention. I have to keep writing these foreach loops to do it. If it were an IEnumerable I would return the IEnumerable. If it were C++ I could write a macro to do it.

What's best practice?

Richard Hunt
  • 303
  • 2
  • 10

2 Answers2

13

If it were an IEnumerable I would return the IEnumerable.

Well, you can just do the same thing with IAsyncEnumerable (note that the async is removed):

IAsyncEnumerable<string> MyFunction()
{
 // ...do some code...

 // Return all elements of the whole stream from the enumerator
 return MyStringEnumerator();
}

However, there's an important semantic consideration here. When calling an enumerator method, the ...do some code... will be executed immediately, and not when the enumerator is enumerated.

// (calling code)
var enumerator = MyFunction(); // `...do some code...` is executed here
...
await foreach (var s in enumerator) // it's not executed here when getting the first `s`
  ...

This is true for both synchronous and asynchronous enumerables.

If you want ...do some code... to be executed when the enumerator is enumerated, then you'll need to use the foreach/yield loop to get the deferred execution semantics:

async IAsyncEnumerable<string> MyFunction()
{
 // ...do some code...

 // Return all elements of the whole stream from the enumerator
 await foreach(var s in MyStringEnumerator())
   yield return s;
}

And you would have to use the same pattern in the synchronous world if you wanted deferred execution semantics with a synchronous enumerable, too:

IEnumerable<string> ImmediateExecution()
{
 // ...do some code...

 // Return all elements of the whole stream from the enumerator
 return MyStringEnumerator();
}

IEnumerable<string> DeferredExecution()
{
 // ...do some code...

 // Return all elements of the whole stream from the enumerator
 foreach(var s in MyStringEnumerator())
   yield return s;
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Will cancellation token semantics and using EnumeratorCancellation attribute work correctly with both immediate and deferred options? Perhaps examples above could be expanded to show this? – Yevgeniy P Mar 09 '21 at 03:04
  • @YevgeniyP: A more real-world solution would take `[EnumeratorCancellation] CancellationToken` and pass it through. – Stephen Cleary Mar 09 '21 at 03:13
2

Returning Task<IAsyncEnumerable<Obj>> from the calling method seems to work

async IAsyncEnumerable<string> MyStringEnumerator();

async Task<IAsyncEnumerable<string>> MyFunction()
{
    await Something();

    return MyStringEnumerator();
}

You'll then need to await MyFunction(). So to use in an async foreach would be

await foreach (string s in await MyFunction()) {}
Craig Miller
  • 543
  • 8
  • 9