Skip to content

Commit b9548f1

Browse files
RexJaeschkeBillWagner
authored andcommitted
Add optional and parameter array params feature
1 parent cd0da70 commit b9548f1

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
@@ -5618,22 +5618,7 @@ anonymous_function_signature
56185618
;
56195619
56205620
explicit_anonymous_function_signature
5621-
: '(' explicit_anonymous_function_parameter_list? ')'
5622-
;
5623-
5624-
explicit_anonymous_function_parameter_list
5625-
: explicit_anonymous_function_parameter
5626-
(',' explicit_anonymous_function_parameter)*
5627-
;
5628-
5629-
explicit_anonymous_function_parameter
5630-
: attributes? 'scoped'? anonymous_function_parameter_modifier? type identifier
5631-
;
5632-
5633-
anonymous_function_parameter_modifier
5634-
: 'ref'
5635-
| 'out'
5636-
| 'in'
5621+
: '(' parameter_list? ')'
56375622
;
56385623
56395624
implicit_anonymous_function_signature
@@ -5670,6 +5655,10 @@ A non-`static` local function or non-`static` anonymous function can capture sta
56705655
> *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*
56715656
<!-- markdownlint-enable MD028 -->
56725657
5658+
A *lambda_expression* shall not contain any *parameter_modifier*s with the `this` modifier.
5659+
5660+
An *anonymous_method_expression* shall not contain any *default_argument*s or *parameter_array*s.
5661+
56735662
When recognising an *anonymous_function_body* if both the *null_conditional_invocation_expression* and *expression* alternatives are applicable then the former shall be chosen.
56745663

56755664
> *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*
@@ -5731,6 +5720,8 @@ A *block* body of an anonymous function is always reachable ([§13.2](statements
57315720
> delegate (int x) { return x + 1; } // Anonymous method expression
57325721
> static delegate (int x) { return x + 1; } // static anonymous method expression
57335722
> delegate { return 1 + 1; } // Parameter list omitted
5723+
> var addWithDefault = (int addTo = 2) => addTo + 1;
5724+
> var counter = (params int[] xs) => xs.Length;
57345725
> var concat = string ([DisallowNull] string a, [DisallowNull] string b) => a + b;
57355726
> Func<string, int?> parse = [X][return: Y] ([Z] s)
57365727
> => (s is not null) ? int.Parse(s) : null;
@@ -5748,6 +5739,7 @@ The behavior of *lambda_expression*s and *anonymous_method_expression*s is the s
57485739
- Only *lambda_expression*s have conversions to compatible expression tree types ([§8.6](types.md#86-expression-tree-types)).
57495740
- Only *lambda_expression* parameters may containscoped’.
57505741
- Only *lambda_expression*s may have *attributes* and explicit return types.
5742+
- An *anonymous_method_expression* may not contain any *default_argument*s or *parameter_array*s.
57515743
57525744
The contextual keyword `var` shall not be used as an explicit return type in a *lambda_expression*.
57535745
@@ -5761,9 +5753,7 @@ The *anonymous_function_signature* of an anonymous function defines the names an
57615753
57625754
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.
57635755
5764-
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.
5765-
5766-
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)).
5756+
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)).
57675757
57685758
### 12.22.3 Anonymous function bodies
57695759
@@ -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)