Feature Request: Syntactic Sugar to yield
the whole of an IEnumerable
directly.
#5303
Replies: 12 comments 48 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.
-
I understand that the reason this hasn't been implemented is because there's a quadratic complexity issue, and that not implementing this is meant to encourage devs to do something else. But most devs are just going to write it out anyway, and then be annoyed that Javascript is nicer to use. Might a better way be to actually implement it, detect possible recursion at compile time (is this possible?), and then yield a warning/error if they used recursion? Thus, actually pointing the dev towards the problem, while helping those who are not using it in a recursive context. |
Beta Was this translation helpful? Give feedback.
-
To piggy-back on what @dnarayan-orepath said: The current answer is "We don't want you to do this because there might be performance issues, so you should do something else." They hand-write the loop because that's the straight forward thing to do. |
Beta Was this translation helpful? Give feedback.
-
@dnarayan-orepath & @TonyValenti
but rather
As it happens, I don't personally agree that the sugar syntax would make it less obvious.
But I believe that is this is the core of the language devs' position w.r.t. "Sugar without underlying change". |
Beta Was this translation helpful? Give feedback.
-
Actually ... that does suggest a potential out for this conversation ... @CyrusNajmabadi if the concern around the sugar syntax is "it hides the perf risk" ... then would you be open to a sugar syntax which actively highlights the perf risk? |
Beta Was this translation helpful? Give feedback.
-
I was about to request this feature with the syntax My usecase is a splitting function where I either need to return an aggregate or all aggregates from the next recursion level: public static IEnumerable<Split> SplitEntries(IEnumerable<Entry> entries, int max)
{
return SplitEntriesRecursive(entries, SplitLevel.Year, max);
}
private enum SplitLevel
{
Year,
Month,
Day,
Hour,
Minute,
Second
}
private static IEnumerable<Split> SplitEntriesRecursive(IEnumerable<Entry> entries, SplitLevel level, int max)
{
var splits = entries
.GroupBy(e => new Split(e.DateTime, level))
.ToDictionary(g => g.Key, g => g.ToList());
foreach (var kvp in splits)
{
if (level == SplitLevel.Second || kvp.Value.Count <= max)
{
yield return kvp.Key;
}
else
{
foreach (var s in SplitEntriesRecursive(kvp.Value, level + 1, max))
{
yield return s;
}
}
}
} Regarding people might introduce performance issues by writing Whereas
|
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.
-
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