Skip to content
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

Create spec for lambda params with modifiers without type name #7369

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions proposals/ref-out-lambda-params.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,39 @@ TryParse<int> parse2 = (string text, out int result) => Int32.TryParse(text, out

### Parameter declaration

Parameter declarations in lambda expressions now permit a single identifier after an `in`/`ref`/`out` modifier on the parameter.
Parameter declarations in lambda expressions with parenthesized parameters now permit a single identifier after an `in`/`ref`/`out` modifier on the parameter. This does not apply to lambda expressions with a single parameter with omitted parentheses.
Rekkonnect marked this conversation as resolved.
Show resolved Hide resolved

The change in the spec will require that, in [the grammar for lambda expressions](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#12191-general), the `implicit_anonymous_function_parameter` rule must be adjusted as follows:
For example,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For example,
For example, these would not be legal.

Also, just do like the first 3 examples.

```csharp
SelfReturnerIn<string> f = in x => x;
SelfReturnerRef<string> g = ref x => x;
SelfReturnerOut<string> h = out x => x;

delegate T SelfReturnerIn<T>(in T t);
delegate T SelfReturnerRef<T>(ref T t);
delegate T SelfReturnerOut<T>(out T t);
```

are all illegal, due to ambiguity with taking the reference of the returned expression in the `ref` case. For consistency, `in` and `out` are also left unsupported and illegal.

The change in the spec will require that, [the grammar for lambda expressions](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#12191-general) must be adjusted as follows:
333fred marked this conversation as resolved.
Show resolved Hide resolved

```diff
implicit_anonymous_function_parameter
- : identifier
implicit_anonymous_function_parameter_list
- : implicit_anonymous_function_parameter
- (',' implicit_anonymous_function_parameter)*
+ : implicit_parenthesized_anonymous_function_parameter
+ (',' implicit_parenthesized_anonymous_function_parameter)*
;

+ implicit_parenthesized_anonymous_function_parameter
+ : identifier
Rekkonnect marked this conversation as resolved.
Show resolved Hide resolved
+ : anonymous_function_parameter_modifier? identifier
;
```

The new grammar rule is added so that this change specifically affects parenthesized lambda expression parameters, leaving lambda expressions with an unparenthesized parameter unsupported.
Rekkonnect marked this conversation as resolved.
Show resolved Hide resolved

The type of the parameters matches the type of the parameter in the target delegate type, including the by-reference modifiers.

Attributes on the parameters will not be affected in any way. Similarly, `async` lambdas will also not be affected from this change.
333fred marked this conversation as resolved.
Show resolved Hide resolved
Expand Down