Feature Request: Syntactic Sugar to yield
the whole of an IEnumerable
directly.
#5303
Replies: 7 comments 25 replies
-
I really hope we see this at some point. It has been asked for so.many.times. |
Beta Was this translation helpful? Give feedback.
-
I've wondered about this too sometimes but I also now wonder if LINQ's |
Beta Was this translation helpful? Give feedback.
-
I don't see why we need this feature just to save writing an |
Beta Was this translation helpful? Give feedback.
-
I think the miscommunication here is in calling this an "edge case", for two reasons:
In other words - the current longhand version of this is already a problem and you need to be careful about how you use it. Until that gets fixed, the syntax being inconvenient is a feature because it discourages use of the pattern and encourages looking for other approaches. The argument isn't "adding syntactic sugar would make it seem like the issue has been fixed", it's "adding syntactic sugar would encourage people to write this kind of code, which they generally shouldn't be doing until the compiler can produce non-awful code for it". |
Beta Was this translation helpful? Give feedback.
-
Hello, I came here from the "I have done a search for similar discussions". I came about looking for a simpler syntax such as The discussion I almost started until I found the existing ones is below. I understand now from reading that there may be some performance cases, but also sharing while I would use even just the "longer" syntax in a common case: In short, this would allow shortcutting:
with simply:
likewise, "items" could just as easily be a method result like:
It's small, but I ran into a situation with an ORM and abstractions of IEnumerables, where I wanted to only treat the The below shows a small sample of how an "external api" might hide some implementation behavior (outputs to console)
Expected Output:
Thanks, |
Beta Was this translation helpful? Give feedback.
-
I would also like this feature, but I understand the reasons for not implementing this yet. However, I'd like to note that the performance problems with the current syntax is either undocumented, or very hard to find. This makes writing If and when syntax is implemented, I'd like |
Beta Was this translation helpful? Give feedback.
-
I think that the performance problems with the current methodology are exactly a strong reason this feature should be implemented. |
Beta Was this translation helpful? Give feedback.
-
See also #378 . This is separating out a syntactic sugar request, from a functional performance enhancement request.
This is NOT a duplicate!
Please consult @CyrusNajmabadi before Closing-As-Duplicate. :)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
If you are writing a
yield
Iterator, and you end up with anIEnumerable<T>
that represents some sub-set of the values you wish to return, then you are obliged to do this:I (and many others) would like there to be a syntactic sugar to express that (even) more tersely:
This would be solely syntactic sugar - it's literally just a short hand for the explicit
foreach
version.(Implementation wise ... we're talking about a Lowering Rewriter)
There would be no changes to performance in any sense.
To support my assertion that this is a syntactic sugar that is desired by C# users:
The possibility of this syntax as a pure sugar syntax came up in #378 , which relates to a very similar concept, but concerns an edge case involving recursive calls, in which the the long-hand code about causes quadratic overhead.
Over in that discussion, the argument presented against this syntax (primarily lead by @CyrusNajmabadi) claims that it is at risk of looking like it's doing something clever about performance, when it's not. i.e. that by including the sugar syntax, which is not confusing in the majority of cases, it will make it look like the recursive edge case has in some way been "fixed" so as to not have the quadratic performance cost.
(I think. ... Note that I don't fully understand this aspect ... @CyrusNajmabadi perhaps you could summarise better?)
The quadratic performance is not itself a concern of this discussion (since we're pitching this a sugar syntax, not claiming to change the performance). But the concern that the syntax could mislead some users does seem a legitimate concern.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(Note: the exact nature of the keyword is not important for this stage of the discussion. I happen to have proposed
yield foreach
, but it could beyield from
,yield all
yield many
,yield*
,yield!
... maybe even justyield
with type analysis identifying that it'syield
ing an IEnumerable in a method that is declared toyield
T
s. The particular keyword isn't important for now - this discussion is to establish whether such a syntax is appropriate/desirable.)Beta Was this translation helpful? Give feedback.
All reactions