Skip to content

Commit 0aa488a

Browse files
RexJaeschkeBillWagner
authored andcommitted
Add optional and parameter array params feature
1 parent 580f604 commit 0aa488a

1 file changed

Lines changed: 12 additions & 22 deletions

File tree

standard/expressions.md

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5613,22 +5613,7 @@ anonymous_function_signature
56135613
;
56145614
56155615
explicit_anonymous_function_signature
5616-
: '(' explicit_anonymous_function_parameter_list? ')'
5617-
;
5618-
5619-
explicit_anonymous_function_parameter_list
5620-
: explicit_anonymous_function_parameter
5621-
(',' explicit_anonymous_function_parameter)*
5622-
;
5623-
5624-
explicit_anonymous_function_parameter
5625-
: attributes? 'scoped'? anonymous_function_parameter_modifier? type identifier
5626-
;
5627-
5628-
anonymous_function_parameter_modifier
5629-
: 'ref'
5630-
| 'out'
5631-
| 'in'
5616+
: '(' parameter_list? ')'
56325617
;
56335618
56345619
implicit_anonymous_function_signature
@@ -5665,6 +5650,10 @@ A non-`static` local function or non-`static` anonymous function can capture sta
56655650
> *Note*: A `static` anonymous function is not required to produce the same delegate instance on each evaluation. See [§10.7.2](conversions.md#1072-evaluation-of-anonymous-function-conversions-to-delegate-types). *end note*
56665651
<!-- markdownlint-enable MD028 -->
56675652
5653+
A *lambda_expression* shall not contain any *parameter_modifier*s with the `this` modifier.
5654+
5655+
An *anonymous_method_expression* shall not contain any *default_argument*s or *parameter_array*s.
5656+
56685657
When recognising an *anonymous_function_body* if both the *null_conditional_invocation_expression* and *expression* alternatives are applicable then the former shall be chosen.
56695658

56705659
> *Note*: The overlapping of, and priority between, alternatives here is solely for descriptive convenience; the grammar rules could be elaborated to remove the overlap. ANTLR, and other grammar systems, adopt the same convenience and so *anonymous_function_body* has the specified semantics automatically. *end note*
@@ -5726,6 +5715,8 @@ A *block* body of an anonymous function is always reachable ([§13.2](statements
57265715
> delegate (int x) { return x + 1; } // Anonymous method expression
57275716
> static delegate (int x) { return x + 1; } // static anonymous method expression
57285717
> delegate { return 1 + 1; } // Parameter list omitted
5718+
> var addWithDefault = (int addTo = 2) => addTo + 1;
5719+
> var counter = (params int[] xs) => xs.Length;
57295720
> var concat = string ([DisallowNull] string a, [DisallowNull] string b) => a + b;
57305721
> Func<string, int?> parse = [X][return: Y] ([Z] s)
57315722
> => (s is not null) ? int.Parse(s) : null;
@@ -5743,6 +5734,7 @@ The behavior of *lambda_expression*s and *anonymous_method_expression*s is the s
57435734
- Only *lambda_expression*s have conversions to compatible expression tree types ([§8.6](types.md#86-expression-tree-types)).
57445735
- Only *lambda_expression* parameters may contain 'scoped'.
57455736
- Only *lambda_expression*s may have *attributes* and explicit return types.
5737+
- An *anonymous_method_expression* may not contain any *default_argument*s or *parameter_array*s.
57465738
57475739
The contextual keyword `var` shall not be used as an explicit return type in a *lambda_expression*.
57485740
@@ -5756,9 +5748,7 @@ The *anonymous_function_signature* of an anonymous function defines the names an
57565748
57575749
If an anonymous function has an *explicit_anonymous_function_signature*, then the set of compatible delegate types and expression tree types is restricted to those that have the same parameter types and modifiers in the same order ([§10.7](conversions.md#107-anonymous-function-conversions)). In contrast to method group conversions ([§10.8](conversions.md#108-method-group-conversions)), contra-variance of anonymous function parameter types is not supported. If an anonymous function does not have an *anonymous_function_signature*, then the set of compatible delegate types and expression tree types is restricted to those that have no output parameters.
57585750
5759-
Note that an *anonymous_function_signature* cannot include a parameter array. Nevertheless, an *anonymous_function_signature* may be compatible with a delegate type whose parameter list contains a parameter array.
5760-
5761-
Note also that conversion to an expression tree type, even if compatible, may still fail at compile-time ([§8.6](types.md#86-expression-tree-types)).
5751+
Note that conversion to an expression tree type, even if compatible, may still fail at compile-time ([§8.6](types.md#86-expression-tree-types)).
57625752
57635753
### 12.22.3 Anonymous function bodies
57645754
@@ -6148,7 +6138,7 @@ object parse4 = (string s) => int.Parse(s);
61486138
Delegate parse5 = (string s) => int.Parse(s);
61496139
```
61506140

6151-
A method group has a natural type if all candidate methods (including extension methods) in the method group have a common signature.
6141+
A method group has a natural type if all candidate methods (including extension methods) in the method group have a common signature including default values and `params` modifiers.
61526142

61536143
```csharp
61546144
var read = Console.Read; // Just one overload; Func<int> inferred
@@ -6164,7 +6154,7 @@ Expression parseExpr = (string s) => int.Parse(s);
61646154
// Expression<Func<string, int>>
61656155
```
61666156

6167-
The natural type of an anonymous function expression or method group is called an ***anonymous function type***. An anonymous function type represents a method signature: the parameter types and ref kinds, and the return type and ref kind. Anonymous function expressions or method groups with the same signature have the same anonymous function type.
6157+
The natural type of an anonymous function expression or method group is called an ***anonymous function type***. An anonymous function type represents a method signature: the parameter types and ref kinds, default values, `params` modifiers, and the return type and ref kind. Anonymous function expressions or method groups with the same signature have the same anonymous function type.
61686158

61696159
Anonymous function types are used in a few specific contexts only:
61706160

@@ -6176,7 +6166,7 @@ An anonymous function type exists only at compile time.
61766166

61776167
The delegate type for the anonymous function or method group with parameter types `P1, ..., Pn` and return type `R` is, as follows:
61786168

6179-
- If any parameter or return value is not by value, or there are more than 16 parameters, or any of the parameter types or return type are not valid type arguments (e.g., `(int* p) => { }`), then the delegate is a synthesized `internal` anonymous delegate type with a signature that matches the anonymous function or method group, and with parameter names `arg1, ..., argn`, or `arg` if a single parameter;
6169+
- If any parameter or return value is not by value, or any parameter is optional or `params`, or there are more than 16 parameters, or any of the parameter types or return type are not valid type arguments (e.g., `(int* p) => { }`), then the delegate is a synthesized `internal` anonymous delegate type with a signature that matches the anonymous function or method group, and with parameter names `arg1, ..., argn`, or `arg` if a single parameter;
61806170
- If `R` is `void`, then the delegate type is `System.Action<P1, ..., Pn>`;
61816171
- Otherwise, the delegate type is `System.Func<P1, ..., Pn, R>`.
61826172

0 commit comments

Comments
 (0)