This project is read-only.
25
Vote

Feature request: yield foreach

description

We all tired of writing this:
foreach (var x in SomeNestedIteratorCall()) {
  yield return x;
}
Instead of:
yield foreach SomeNestedIteratorCall();
Yes, you can't easily do this feature with all of optimizations F# performs (tail yield! and swapping enumerators to reduce complexity of nested enumerables), since it requires some System.Runtime.CompilerServices.* code to support.

But why not provide this feature just as syntax sugar for now?

comments

MobileNow wrote Apr 29, 2014 at 3:42 PM

I would propose keeping the syntax even more simple:

either:
yield all SomeNestedIteratorCall();
or even better:
yield SomeNestedIteratorCall();
With the second option, some simple type checking by the compiler would be enough to determine if it should do a standard yield or the "for each" yield.

ControlFlow wrote Apr 29, 2014 at 5:57 PM

yield SomeNestedIteratorCall();
  • is too ambiguous for parsing and requires lookahead + overloading by type is not a good idea for language construct.
yield all SomeNestedIteratorCall();
  • as far as I know, there is no all keyword/contextual keyword in C#.
C'mon, foreach keyword is so familiar to everyone! :)

AdamSpeight2008 wrote Apr 30, 2014 at 1:59 AM

KrisVDM wrote May 9, 2014 at 1:25 AM

This has been proposed back in 2005, by no less than Bart Jacobs, Erik Meijer, Frank Piessens, and Wolfram Schulte. For more information, see http://research.microsoft.com/en-us/projects/specsharp/iterators.pdf.

Now might be a good time to implement it.

MgSam wrote May 9, 2014 at 8:32 PM

I suggest anyone voting on this also read lwischik's comments in the discussion AdamSpeight2008 linked to.

BlueCode wrote May 12, 2014 at 12:39 PM

I vote for yield foreach.

pdelvo wrote Jul 8, 2014 at 10:35 AM

yield all SomeNestedIteratorCall();
Is a bad idea. This would break existing code if "all" is used as a variable name somewhere. yield foreach would be better