diff --git a/standard/attributes.md b/standard/attributes.md index 89f8822d9..b8ee8b14e 100644 --- a/standard/attributes.md +++ b/standard/attributes.md @@ -162,7 +162,7 @@ The types of positional and named parameters for an attribute class are limited ## 23.3 Attribute specification -Application of a previously defined attribute to a program entity is called ***attribute specification***. An attribute is a piece of additional declarative information that is specified for a program entity. Attributes can be specified at global scope (to specify attributes on the containing assembly or module) and for *type_declaration*s ([§14.8](namespaces.md#148-type-declarations)), *class_member_declaration*s ([§15.3](classes.md#153-class-members)), *interface_member_declaration*s ([§19.4](interfaces.md#194-interface-members)), *struct_member_declaration*s ([§16.3](structs.md#163-struct-members)), *enum_member_declaration*s ([§20.2](enums.md#202-enum-declarations)), *accessor_declaration*s ([§15.7.3](classes.md#1573-accessors)), *event_accessor_declaration*s ([§15.8](classes.md#158-events)), *local_function_declaration*s ([§13.6.4](statements.md#1364-local-function-declarations)), elements of *parameter_list*s ([§15.6.2](classes.md#1562-method-parameters)), elements of *type_parameter_list*s ([§15.2.3](classes.md#1523-type-parameters)), *lambda_expression*s ([§12.22.1](expressions.md#12221-general)), and elements of *explicit_anonymous_function_parameter*s and *implicit_anonymous_function_parameter*s ([§12.22.1](expressions.md#12221-general)). +Application of a previously defined attribute to a program entity is called ***attribute specification***. An attribute is a piece of additional declarative information that is specified for a program entity. Attributes can be specified at global scope (to specify attributes on the containing assembly or module) and for *type_declaration*s ([§14.8](namespaces.md#148-type-declarations)), *class_member_declaration*s ([§15.3](classes.md#153-class-members)), *interface_member_declaration*s ([§19.4](interfaces.md#194-interface-members)), *struct_member_declaration*s ([§16.3](structs.md#163-struct-members)), *enum_member_declaration*s ([§20.2](enums.md#202-enum-declarations)), *accessor_declaration*s ([§15.7.3](classes.md#1573-accessors)), *event_accessor_declaration*s ([§15.8](classes.md#158-events)), *local_function_declaration*s ([§13.6.4](statements.md#1364-local-function-declarations)), elements of *parameter_list*s ([§15.6.2](classes.md#1562-method-parameters)), elements of *type_parameter_list*s ([§15.2.3](classes.md#1523-type-parameters)), *lambda_expression*s ([§12.22.1](expressions.md#12221-general)), and their explicitly or implicitly typed parameters (*fixed_parameter*s, *parameter_array*s, and *implicit_anonymous_function_parameter*s) ([§12.22.1](expressions.md#12221-general)). Attributes are specified in ***attribute section***s. An attribute section consists of a pair of square brackets, which surround a comma-separated list of one or more attributes. The order in which attributes are specified in such a list, and the order in which sections attached to the same program entity are arranged, is not significant. For instance, the attribute specifications `[A][B]`, `[B][A]`, `[A, B]`, and `[B, A]` are equivalent. diff --git a/standard/conversions.md b/standard/conversions.md index 345c7bd8e..e5fefdbd6 100644 --- a/standard/conversions.md +++ b/standard/conversions.md @@ -958,7 +958,7 @@ Specifically, an anonymous function `F` is compatible with a delegate type `D` - If `F` contains an *anonymous_function_signature*, then `D` and `F` have the same number of parameters. - If `F` does not contain an *anonymous_function_signature*, then `D` may have zero or more parameters of any type, as long as no parameter of `D` is an output parameter. -- If `F` has an explicitly typed parameter list, each parameter in `D` has the same modifiers as the corresponding parameter in `F` and an identity conversion exists between the corresponding parameter in `F`. +- If `F` has an explicitly typed parameter list, each parameter in `D` has the same modifiers as the corresponding parameter in `F`, ignoring `params` modifiers and default values, and an identity conversion exists between the corresponding parameter in `F`. - If `F` has an implicitly typed parameter list, `D` has no reference or output parameters. - If `F` has an explicit return type, an identity conversion shall exist from the return type of `F` to the return type of `D`. - If the body of `F` is an expression, and *either* `D` has a void return type *or* `F` is async and `D` has a `«TaskType»` return type ([§15.14.1](classes.md#15141-general)), then when each parameter of `F` is given the type of the corresponding parameter in `D`, the body of `F` is a valid expression (w.r.t [§12](expressions.md#12-expressions)) that would be permitted as a *statement_expression* ([§13.7](statements.md#137-expression-statements)). @@ -1046,6 +1046,13 @@ Specifically, an anonymous function `F` is compatible with a delegate type `D` A lambda expression `F` is compatible with an expression tree type `Expression` if `F` is compatible with the delegate type `D`. This does not apply to anonymous methods, only lambda expressions. +When a lambda expression `F` is converted to a delegate type `D` or an expression tree type `Expression`, a warning may be issued if any corresponding parameter pair meets either of the following conditions: + +- The parameter in `F` has a *default_argument*, and the parameter in `D` has no default value or has a different default value. +- The parameter in `F` has a `params` modifier, and the parameter in `D` does not. + +These warnings are considered after any applicable overload resolution has selected a function member; they do not affect compatibility or overload resolution. Invocation of a delegate of type `D` is governed by the default values and `params` modifiers of `D`. + Anonymous functions may influence overload resolution, and participate in type inference. See [§12.6](expressions.md#126-function-members) for further details. ### 10.7.2 Evaluation of anonymous function conversions to delegate types diff --git a/standard/expressions.md b/standard/expressions.md index fc6f7f8ee..80bee1868 100644 --- a/standard/expressions.md +++ b/standard/expressions.md @@ -5721,22 +5721,7 @@ anonymous_function_signature ; explicit_anonymous_function_signature - : '(' explicit_anonymous_function_parameter_list? ')' - ; - -explicit_anonymous_function_parameter_list - : explicit_anonymous_function_parameter - (',' explicit_anonymous_function_parameter)* - ; - -explicit_anonymous_function_parameter - : attributes? 'scoped'? anonymous_function_parameter_modifier? type identifier - ; - -anonymous_function_parameter_modifier - : 'ref' - | 'out' - | 'in' + : '(' parameter_list? ')' ; implicit_anonymous_function_signature @@ -5773,6 +5758,10 @@ A non-`static` local function or non-`static` anonymous function can capture sta > *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* +An anonymous function shall not contain any *parameter_modifier*s with the `this` modifier. + +An *anonymous_method_expression* shall not contain any *default_argument*s or *parameter_array*s. + When recognising an *anonymous_function_body* if both the *null_conditional_invocation_expression* and *expression* alternatives are applicable then the former shall be chosen. > *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* @@ -5801,7 +5790,7 @@ An anonymous function with the `async` modifier is an async function and follows The parameters of an anonymous function in the form of a *lambda_expression* can be explicitly or implicitly typed. In an explicitly typed parameter list, the type of each parameter is explicitly stated. In an implicitly typed parameter list, the types of the parameters are inferred from the context in which the anonymous function occurs—specifically, when the anonymous function is converted to a compatible delegate type or expression tree type, that type provides the parameter types ([§10.7](conversions.md#107-anonymous-function-conversions)). -In a *lambda_expression* with a single, implicitly typed parameter, the parentheses may be omitted from the parameter list, provided that *lambda_expression* has no *attributes* on the whole expression or on any of its *explicit_anonymous_function_parameter*s, and it has no explicit return type. In other words, an anonymous function of the form +In a *lambda_expression* with a single, implicitly typed parameter, the parentheses may be omitted from the parameter list, provided that *lambda_expression* has no *attributes* on the whole expression or on its *implicit_anonymous_function_parameter*, and it has no explicit return type. In other words, an anonymous function of the form ```csharp ( «param» ) => «expr» @@ -5815,7 +5804,7 @@ can be abbreviated to The parameter list of an anonymous function in the form of an *anonymous_method_expression* is optional. If given, the parameters shall be explicitly typed. If not, the anonymous function is convertible to a delegate with any parameter list not containing output parameters. -It is a compile error for any *explicit_anonymous_function_parameter* of an anonymous function in the form of an *anonymous_method_expression* to have *attributes*. +It is a compile-time error for any *fixed_parameter* of an *anonymous_method_expression* to have *attributes*. A *block* body of an anonymous function is always reachable ([§13.2](statements.md#132-end-points-and-reachability)). @@ -5834,6 +5823,8 @@ A *block* body of an anonymous function is always reachable ([§13.2](statements > delegate (int x) { return x + 1; } // Anonymous method expression > static delegate (int x) { return x + 1; } // Static anonymous method expression > delegate { return 1 + 1; } // Parameter list omitted +> var addWithDefault = (int addTo = 2) => addTo + 1; +> var counter = (params int[] xs) => xs.Length; > var concat = string ([DisallowNull] string a, [DisallowNull] string b) => a + b; > Func parse = [X][return: Y] ([Z] s) > => (s is not null) ? int.Parse(s) : null; @@ -5851,12 +5842,13 @@ The behavior of *lambda_expression*s and *anonymous_method_expression*s is the s - Only *lambda_expression*s have conversions to compatible expression tree types ([§8.6](types.md#86-expression-tree-types)). - Only *lambda_expression* parameters may contain ‘scoped’. - Only *lambda_expression*s may have *attributes* and explicit return types. +- An *anonymous_method_expression* may not contain any *default_argument*s or *parameter_array*s. The contextual keyword `var` shall not be used as an explicit return type in a *lambda_expression*. ### 12.22.2 Anonymous function signatures -In an anonymous function parameter list, if two or more of the *identifier*s occurring directly in its *explicit_anonymous_function_parameter*s or *implicit_anonymous_function_parameter*s are *discard_token*s, each corresponding parameter is a ***discard parameter*** ([§9.2.9.2](variables.md#9292-discards)). All other parameters are non-discard parameters, whose names are specified by their *identifier*s. +In an anonymous function parameter list, if two or more of the *identifier*s occurring directly in its *fixed_parameter*s, *parameter_array*s, or *implicit_anonymous_function_parameter*s are *discard_token*s, each corresponding parameter is a ***discard parameter*** ([§9.2.9.2](variables.md#9292-discards)). All other parameters are non-discard parameters, whose names are specified by their *identifier*s. The *anonymous_function_signature* of an anonymous function defines the names of the non-discard parameters and optionally the types and *attributes* of all the parameters for the anonymous function. The scope of the non-discard parameters of the anonymous function is the *anonymous_function_body* ([§7.6](basic-concepts.md#76-scopes)) and `nameof` expressions in attributes placed on the anonymous function or its parameters. Together with the parameter list (if given) the anonymous-method-body constitutes a declaration space ([§7.2](basic-concepts.md#72-declarations)). It is thus a compile-time error for the name of a non-discard parameter of the anonymous function to match the name of a local variable, local constant or parameter whose scope includes the *anonymous_method_expression* or *lambda_expression*. @@ -5884,9 +5876,7 @@ The *anonymous_function_signature* of an anonymous function defines the names of 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. -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. - -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)). +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)). ### 12.22.3 Anonymous function bodies @@ -6252,11 +6242,11 @@ An anonymous function `F` shall always be converted to a delegate type `D` or ### 12.22.8 Anonymous function type -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. An anonymous function type exists only at compile time and does not appear in source or metadata. Anonymous function expressions or method groups with the same signature have the same anonymous function type. +The natural type of an anonymous function expression or method group is called an ***anonymous function type***. An anonymous function type represents a signature consisting of the parameter types and ref kinds, whether each parameter is required or optional, default values, `params` modifiers, and the return type and ref kind. An anonymous function type exists only at compile time and does not appear in source or metadata. Anonymous function expressions or method groups with the same signature have the same anonymous function type. An anonymous function expression has a ***natural type*** if its parameter list is present, its parameter types are explicit, and its return type is either explicit or can be inferred ([§12.6.3.14](expressions.md#126314-inferred-return-type)). For this purpose, a non-async anonymous function whose body is an expression classified as nothing, or a block with no `return` statements specifying expressions, has inferred return type `void`. -A method group has a natural type if all candidate methods (including extension methods) in the method group have a common signature. +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. An anonymous function type is determined independently of a conversion. When a delegate type is needed, the corresponding delegate type is determined from this signature as specified below. In particular, that delegate type, not the anonymous function type, is used as the type of an implicitly typed local variable ([§13.6.2.2](statements.md#13622-implicitly-typed-local-variable-declarations)). @@ -6306,13 +6296,13 @@ Anonymous function types are used in a few specific contexts only: The delegate type for the anonymous function or method group with parameter types `P1, ..., Pn` and return type `R` is, as follows: -- 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; +- 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; - If `R` is `void`, then the delegate type is `System.Action`; - Otherwise, the delegate type is `System.Func`. > *Note*: A future version of this specification might allow more signatures to bind to `System.Action<>` and `System.Func<>` types (e.g., if `ref struct` types are allowed type arguments). *end note* -If two anonymous functions or method groups in the same compilation require synthesized delegate types with the same parameter types and modifiers, and the same return type and modifiers, the compiler shall use the same synthesized delegate type. +If two anonymous functions or method groups in the same compilation require synthesized delegate types with the same parameter types and modifiers (including `params`), corresponding parameters that are either both required or both optional with the same default values, and the same return type and modifiers, the compiler shall use the same synthesized delegate type. Parameter names do not affect this comparison. > *Example*: Explicit return types can provide bounds even when parameter types have not yet been inferred. Anonymous function types can also provide bounds when there is no target delegate type: >