Allow iterator/async iterator lambdas/anonymous methods #9393
Replies: 3 comments 9 replies
-
cc: @jaredpar |
Beta Was this translation helpful? Give feedback.
-
How about non-async iterators? |
Beta Was this translation helpful? Give feedback.
-
When we first rejected lambda iterators many years ago, we did not have local functions, and lambdas did not have return types or inferred delegate types. We were looking for statement-scoped ways of declaring iterators, and lambdas were too roundabout for that: First create a delegate object by explicitly converting the lambda to a clunky type name, then immediately invoke it to get the enumerable. Local functions ended up being the more straightforward design for this. However, the scenarios now are compelling, and I think the design would be straightforward. I am certainly in favor. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Champion issue: #9467
Currently, trying to use
yield
inside a lambda triggers CS1621: "The yield statement cannot be used inside an anonymous method or lambda expression".One main motivator for this is being able to work with IAsyncEnumerable (and potentially IEnumerable) within ASP.NET Minimal API delegates:
Because of the current restriction, the body above must be split out to a named function, just in order to be able to use
yield
. As IAsyncEnumerable becomes more popular, there may be more cases where async iterator lambdas could be helpful for working with it.Given that we can now explicitly specify the return value of lambdas, it may make more sense to allow this feature. This may also fit within the general move of making lambdas/anonymous functions support more features that regular methods do (static modifier, attributes, explicit return types...).
/cc @MadsTorgersen @stephentoub @davidfowl
Beta Was this translation helpful? Give feedback.
All reactions