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?