Discussion: static local functions and implicit parameters #3475
Replies: 3 comments
-
Wouldn't this be confusing when it comes to mutating such variables? Currently, there are two ways to share values between a local function and its parent:
This would add a third way, one which is implicit, but makes the variables separate and that feels inconsistent to me. In code: int i;
void F1() => i++;
i = 0;
F1();
Console.WriteLine(i); // 1, as expected
void F2(int i) => i++;
i = 0;
F2(i);
Console.WriteLine(i); // 0, as expected
void F3(ref int i) => i++;
i = 0;
F3(ref i);
Console.WriteLine(i); // 1, as expected
void F4(i) => i++;
i = 0;
F4();
Console.WriteLine(i); // 0 ??? Though making the variables shared could require a capture object, which is exactly what you're trying to avoid. And I can't think of another reasonable way to decrease the confusion either. |
Beta Was this translation helpful? Give feedback.
-
You can think of it as the same way optional parameters behave, where the default value is inserted at the call-site if absent. |
Beta Was this translation helpful? Give feedback.
-
Oh I guess now I see what you mean. I think the only way to prevent that is to make all implicit parameters readonly. Perhaps we could permit |
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.
-
In order to make a static local function we need to explicitly pass all intended captures as arguments and declare a parameter for each,
We could simplify the above to the following, using parameter list as a kind of "capture list"
At the point of invocation all arguments must be fully assigned and in the scope.
Each argument declares a parameter with the same name. For
this
, we use@this
for the name (in metadata and if used with named args) but we still can usethis
to refer to that parameter.An advantage of using
this
is that we could possibly permit an impilcitthis
reference in the body.These parameters act like optional parameters which default to a location with the specified name, therefore they should always come after explicit parameters.
In this case, we need to define most unchanging values as the rightmost parameters for them to be passed implicitly everywhere. For instance,
this
should always come last.Beta Was this translation helpful? Give feedback.
All reactions