Skip to content

Commit 6fbfd50

Browse files
authored
Add optional and parameter array params feature
1 parent 50e321b commit 6fbfd50

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
@@ -5629,22 +5629,7 @@ anonymous_function_signature
56295629
;
56305630
56315631
explicit_anonymous_function_signature
5632-
: '(' explicit_anonymous_function_parameter_list? ')'
5633-
;
5634-
5635-
explicit_anonymous_function_parameter_list
5636-
: explicit_anonymous_function_parameter
5637-
(',' explicit_anonymous_function_parameter)*
5638-
;
5639-
5640-
explicit_anonymous_function_parameter
5641-
: attributes? 'scoped'? anonymous_function_parameter_modifier? type identifier
5642-
;
5643-
5644-
anonymous_function_parameter_modifier
5645-
: 'ref'
5646-
| 'out'
5647-
| 'in'
5632+
: '(' parameter_list? ')'
56485633
;
56495634
56505635
implicit_anonymous_function_signature
@@ -5677,6 +5662,10 @@ A non-`static` local function or non-`static` anonymous function can capture sta
56775662

56785663
Removing the `static` modifier from an anonymous function in a valid program does not change the meaning of the program.
56795664

5665+
A *lambda_expression* shall not contain any *parameter_modifier*s with the `this` modifier.
5666+
5667+
An *anonymous_method_expression* shall not contain any *default_argument*s or *parameter_array*s.
5668+
56805669
When recognising an *anonymous_function_body* if both the *null_conditional_invocation_expression* and *expression* alternatives are applicable then the former shall be chosen.
56815670

56825671
<!-- markdownlint-disable MD028 -->
@@ -5736,6 +5725,8 @@ A *block* body of an anonymous function is always reachable ([§13.2](statements
57365725
> async (t1,t2) => await t1 + await t2 // Async
57375726
> static delegate (int x) { return x + 1; } // Anonymous method expression
57385727
> delegate { return 1 + 1; } // Parameter list omitted
5728+
> var addWithDefault = (int addTo = 2) => addTo + 1;
5729+
> var counter = (params int[] xs) => xs.Length;
57395730
> var concat = string ([DisallowNull] string a, [DisallowNull] string b) => a + b;
57405731
> Func<string, int?> parse = [X][return: Y] ([Z] s)
57415732
> => (s is not null) ? int.Parse(s) : null;
@@ -5753,6 +5744,7 @@ The behavior of *lambda_expression*s and *anonymous_method_expression*s is the s
57535744
- Only *lambda_expression*s have conversions to compatible expression tree types ([§8.6](types.md#86-expression-tree-types)).
57545745
- Only *lambda_expression* parameters may contain 'scoped'.
57555746
- Only *lambda_expression*s may have *attributes* and explicit return types.
5747+
- An *anonymous_method_expression* may not contain any *default_argument*s or *parameter_array*s.
57565748
57575749
### 12.22.2 Anonymous function signatures
57585750
@@ -5762,9 +5754,7 @@ The *anonymous_function_signature* of an anonymous function defines the names an
57625754
57635755
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.
57645756
5765-
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.
5766-
5767-
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)).
5757+
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)).
57685758
57695759
### 12.22.3 Anonymous function bodies
57705760
@@ -6153,7 +6143,7 @@ object parse4 = (string s) => int.Parse(s);
61536143
Delegate parse5 = (string s) => int.Parse(s);
61546144
```
61556145

6156-
A method group has a natural type if all candidate methods (including extension methods) in the method group have a common signature.
6146+
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.
61576147

61586148
```csharp
61596149
var read = Console.Read; // Just one overload; Func<int> inferred
@@ -6169,7 +6159,7 @@ Expression parseExpr = (string s) => int.Parse(s);
61696159
// Expression<Func<string, int>>
61706160
```
61716161

6172-
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.
6162+
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.
61736163

61746164
Anonymous function types are used in a few specific contexts only:
61756165

@@ -6181,7 +6171,7 @@ An anonymous function type exists only at compile time.
61816171

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

6184-
- 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;
6174+
- 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;
61856175
- If `R` is `void`, then the delegate type is `System.Action<P1, ..., Pn>`;
61866176
- Otherwise, the delegate type is `System.Func<P1, ..., Pn, R>`.
61876177

0 commit comments

Comments
 (0)