Proposal: yield => <expression>;
as supplement to yield return <value>;
for ref struct
etc. support
#9111
Replies: 2 comments 13 replies
-
The expression under For usages that need to produce |
Beta Was this translation helpful? Give feedback.
-
I'm not sure i understand why the => is needed. Will this not become possible when ref struct can implement interfaces, allowing the compiler to create a ref struct state machine? |
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.
-
The following program or similar programs with an
IEnumerable<T>
where T is aref struct
will either compile fine, crash the compiler or crash at runtime. The issue is of course the state machine trying to store theref struct
as a member.Sharplab below will show a compiler error or similar.
https://sharplab.io/#v2:D4AQTAjAsAUCAMACEEAsBuWsBmB7ATgKYCGAxgBaIAUAbsfogM4AOxAdogJYcCibArgFtC+YgBdCAZVZtGVAJTzYAb1iJ1yCAE4qLdgDoAKrklj83AOYL5mGAF8scCADZNAZgA8AJRIATAPJsADYAntLsHhT0AHzRiHxCIuJSMnJKMKowGpoQyADsiABEABKEQUG4hfoAgozhbAq22Si5IAWFAOoEQb5VtfWNsA4wQA=
I would like to propose the idea to add syntax support for yielding "expressions", that is
Current
of the enumerator/async enumerator will beCurrent => <expression>;
and whatever is used in expression not part of other members be added to members of state machine.Above is just an example to show usage. A motivational example for code in BCL that might be more easily possible with this could be
EnumerateLines()
method onTextReader
like:The point of the idea is to open up C# for defining
ref struct
iterators, there are many use cases all it takes is a bit of imagination.Beta Was this translation helpful? Give feedback.
All reactions