-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion: async/iterator local function captures #11585
Comments
@ljw1004 You may be interested in this. |
A simple |
@svick Note that this only happens when your local function is async or iterator; otherwise a public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate) {
if (source == null) ...
if (predicate == null) ...
IEnumerable<T> WhereImpl(IEnumerable<T> _source, Func<T, bool> _predicate) {
foreach (var item in _source) {
if (_predicate(item))
yield return item;
}
}
return WhereImpl(source, predicate);
} Since this will be a common use case (capturing variables at callsite via parameters) I think it'd be nice to be able to use same parameter names as the outer function for the local one. |
@gafter Can I reword this issue as a feature request to allow local function's parameters to shadow the outer method's parameter names, or is that a no-go? |
@alrz I think repurposing this issue would be confusing. Please create a fresh feature request and include the motivating example(s). |
A common use case for local functions is to avoid state machine creation in async or iterator functions before parameter validation, etc. But if you capture any variable or parameter, the corresponding fields will be added to a
class
that we were intended to avoid creating in the first place. The only workaround is to pass every parameter to the local function which has to have a different name.It would be nice to generate a separate
struct
for captured variables in async/iterator local functions, or permit a local function to shadow outer function's parameters.Related: #11308
The text was updated successfully, but these errors were encountered: