Allow to mix return
and yield return
in certain conditions
#7551
Answered
by
HaloFour
karakasa
asked this question in
Language Ideas
-
I think it can be beneficial if I searched in the discussions and no one seemed to bring this up. For example: public IEnumerable<int> DoSomething(this IEnumerable<int> src)
{
if (src is int[] srcArray)
return DoSomethingArray(srcArray);
// Fallback
yield return src.First();
}
private IEnumerable<int> DoSomethingArray(int[] src)
{
return new int[]{ src[0] };
} Though I acknowledge that the feature can be very challenging to implement, if allowed to be combined with |
Beta Was this translation helpful? Give feedback.
Answered by
HaloFour
Sep 22, 2023
Replies: 1 comment 4 replies
-
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That implies that such a feature could only ever be used if the compiler can prove that it's the only elements that could only ever be yielded down that path. But, even if it could, the compiler should definitely never do that implicitly as that radically changes the behavior of how iterator methods are expected to run. If there were to be an "eager" portion of an iterator that should have to be explicitly specified, and the compiler team has already considered a couple of proposals to that have have landed on local functions being a perfectly suitable way to accomplish that behavior: