Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 21 additions & 15 deletions standard/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2169,9 +2169,9 @@ Grammar notes:

> *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 *method_body* has the specified semantics automatically. *end note*

A *method_declaration* may include a set of *attributes* ([§23](attributes.md#23-attributes)) and one of the permitted kinds of declared accessibility ([§15.3.6](classes.md#1536-access-modifiers)), the `new` ([§15.3.5](classes.md#1535-the-new-modifier)), `static` ([§15.6.3](classes.md#1563-static-and-instance-methods)), `virtual` ([§15.6.4](classes.md#1564-virtual-methods)), `override` ([§15.6.5](classes.md#1565-override-methods)), `sealed` ([§15.6.6](classes.md#1566-sealed-methods)), `abstract` ([§15.6.7](classes.md#1567-abstract-methods)), `extern` ([§15.6.8](classes.md#1568-external-methods)) and `async` ([§15.14](classes.md#1514-async-functions)). Additionally a *method_declaration* that is contained directly by a *struct_declaration* may include the `readonly` modifier ([§16.6.12](structs.md#16612-methods)).
A *method_declaration* may include a set of *attributes* ([§23](attributes.md#23-attributes)) and one of the permitted kinds of declared accessibility ([§15.3.6](classes.md#1536-access-modifiers)), the `new` ([§15.3.5](classes.md#1535-the-new-modifier)), `static` ([§15.6.3](classes.md#1563-static-and-instance-methods)), `virtual` ([§15.6.4](classes.md#1564-virtual-methods)), `override` ([§15.6.5](classes.md#1565-override-methods)), `sealed` ([§15.6.6](classes.md#1566-sealed-methods)), `abstract` ([§15.6.7](classes.md#1567-abstract-methods)), `extern` ([§15.6.8](classes.md#1568-external-methods)) and `async` ([§15.14](classes.md#1514-async-functions)) modifiers. Additionally a *method_declaration* that is contained directly by a *struct_declaration* may include the `readonly` modifier ([§16.6.12](structs.md#16612-methods)).

A declaration has a valid combination of modifiers if all of the following are true:
A *method_declaration* has a valid combination of modifiers if all of the following are true. (These rules are modified slightly in the context of an interface; see [§19.4.1](interfaces.md#1941-general).):

- The declaration includes a valid combination of access modifiers ([§15.3.6](classes.md#1536-access-modifiers)).
- The declaration does not include the same modifier multiple times.
Expand Down Expand Up @@ -2200,7 +2200,7 @@ A generic *method_declaration*, either with an `override` modifier, or for an ex

The *member_name* specifies the name of the method. Unless the method is an explicit interface member implementation ([§19.6.2](interfaces.md#1962-explicit-interface-member-implementations)), the *member_name* is simply an *identifier*.

For an explicit interface member implementation, the *member_name* consists of an *interface_type* followed by a “`.`” and an *identifier*. In this case, the declaration shall include no modifiers other than (possibly) `extern` or `async`.
For an explicit interface member implementation, the *member_name* consists of an *interface_type* followed by a “`.`” and an *identifier*. The permitted modifiers are specified in [§19.6.2](interfaces.md#1962-explicit-interface-member-implementations).

The optional *parameter_list* specifies the parameters of the method ([§15.6.2](classes.md#1562-method-parameters)).

Expand Down Expand Up @@ -4963,6 +4963,9 @@ operator_modifier
: 'public'
| 'static'
| 'extern'
| 'abstract'
| 'virtual'
| 'sealed'
| unsafe_modifier // unsafe code support
;

Expand All @@ -4973,7 +4976,8 @@ operator_declarator
;

unary_operator_declarator
: type 'operator' overloadable_unary_operator '(' fixed_parameter ')'
: type (interface_type '.')? 'operator' overloadable_unary_operator
'(' fixed_parameter ')'
;

logical_negation_operator
Expand All @@ -4985,7 +4989,7 @@ overloadable_unary_operator
;

binary_operator_declarator
: type 'operator' overloadable_binary_operator
: type (interface_type '.')? 'operator' overloadable_binary_operator
'(' fixed_parameter ',' fixed_parameter ')'
;

Expand All @@ -4995,8 +4999,8 @@ overloadable_binary_operator
;

conversion_operator_declarator
: 'implicit' 'operator' type '(' fixed_parameter ')'
| 'explicit' 'operator' type '(' fixed_parameter ')'
: 'implicit' (interface_type '.')? 'operator' type '(' fixed_parameter ')'
| 'explicit' (interface_type '.')? 'operator' type '(' fixed_parameter ')'
;

operator_body
Expand All @@ -5014,14 +5018,16 @@ There are three categories of overloadable operators: Unary operators ([§15.10.

The *operator_body* is either a semicolon, a block body ([§15.6.1](classes.md#1561-general)) or an expression body ([§15.6.1](classes.md#1561-general)). A block body consists of a *block*, which specifies the statements to execute when the operator is invoked. The *block* shall conform to the rules for value-returning methods described in [§15.6.11](classes.md#15611-method-body). An expression body consists of `=>` followed by an expression and a semicolon, and denotes a single expression to perform when the operator is invoked.

For `extern` operators, the *operator_body* consists simply of a semicolon. For all other operators, the *operator_body* is either a block body or an expression body.
For `extern` operators, the *operator_body* consists simply of a semicolon. For an abstract operator declaration in an interface ([§19.4.7](interfaces.md#1947-interface-operators)), the *operator_body* is a semicolon. For all other operators, the *operator_body* is either a block body or an expression body.

The following rules apply to all operator declarations:

- An operator declaration shall include both a `public` and a `static` modifier.
- An operator declaration shall include a `static` modifier. Except in an explicit interface member implementation, an operator declared in a class or struct shall also include a `public` modifier. Operators declared in interfaces are subject to [§19.4.7](interfaces.md#1947-interface-operators).
- The `abstract`, `virtual`, and `sealed` modifiers are permitted only on operators declared in interfaces.
- An operator declarator that includes an *interface_type* followed by a “`.`” is an explicit interface member implementation and is subject to [§19.6.2](interfaces.md#1962-explicit-interface-member-implementations). The operand and result type restrictions in [§15.10.2](classes.md#15102-unary-operators), [§15.10.3](classes.md#15103-binary-operators), and [§15.10.4](classes.md#15104-conversion-operators) apply to the implemented interface declaration rather than to its explicit implementation.
- The parameters of an operator shall have no modifiers other than `in`.
- The signature of an operator ([§15.10.2](classes.md#15102-unary-operators), [§15.10.3](classes.md#15103-binary-operators), [§15.10.4](classes.md#15104-conversion-operators)) shall differ from the signatures of all other operators declared in the same class.
- All types referenced in an operator declaration shall be at least as accessible as the operator itself ([§7.4.5](basic-concepts.md#745-accessibility-constraints)).
- Except in an explicit interface member implementation, the signature of an operator ([§15.10.2](classes.md#15102-unary-operators), [§15.10.3](classes.md#15103-binary-operators), [§15.10.4](classes.md#15104-conversion-operators)) shall differ from the signatures of all other non-explicitly implemented operators declared in the same type.
- Except in an explicit interface member implementation, all types referenced in an operator declaration shall be at least as accessible as the operator itself ([§7.4.5](basic-concepts.md#745-accessibility-constraints)).
- It is an error for the same modifier to appear multiple times in an operator declaration.

Each operator category imposes additional restrictions, as described in the following subclauses.
Expand Down Expand Up @@ -5106,17 +5112,17 @@ A conversion operator declaration that includes the `explicit` keyword introduce

A conversion operator converts from a source type, indicated by the parameter type of the conversion operator, to a target type, indicated by the return type of the conversion operator.

For a given source type `S` and target type `T`, if `S` or `T` are nullable value types, let `S₀` and `T₀` refer to their underlying types; otherwise, `S₀` and `T₀` are equal to `S` and `T` respectively. A class or struct is permitted to declare a conversion from a source type `S` to a target type `T` only if all of the following are true:
For a given source type `S` and target type `T`, if `S` or `T` are nullable value types, let `S₀` and `T₀` refer to their underlying types; otherwise, `S₀` and `T₀` are equal to `S` and `T` respectively. A class, struct, or interface is permitted to declare a conversion from a source type `S` to a target type `T` only if all of the following are true, except for explicit interface member implementations as specified in [§15.10.1](classes.md#15101-general):

- `S₀` and `T₀` are different types.

- Either `S₀` or `T₀` is the instance type of the class or struct that contains the operator declaration.
- Either `S₀` or `T₀` is the instance type of the class or struct that contains the operator declaration, or is a type parameter that counts as the instance type of the containing interface as specified in [§19.4.7](interfaces.md#1947-interface-operators).

- Neither `S₀` nor `T₀` is an *interface_type*.

- Excluding user-defined conversions, a conversion does not exist from `S` to `T` or from `T` to `S`.

For the purposes of these rules, any type parameters associated with `S` or `T` are considered to be unique types that have no inheritance relationship with other types, and any constraints on those type parameters are ignored.
For the purposes of these rules, any type parameters associated with `S` or `T` are considered to be unique types that have no inheritance relationship with other types. Their constraints are considered only to determine whether a type parameter counts as the instance type of the containing interface.

> *Example*: In the following:
>
Expand All @@ -5136,7 +5142,7 @@ For the purposes of these rules, any type parameters associated with `S` or `T
>
> *end example*

From the second rule, it follows that a conversion operator shall convert either to or from the class or struct type in which the operator is declared.
From the second rule, it follows that a conversion operator declared in a class or struct, other than an explicit interface member implementation, shall convert either to or from that class or struct type.

> *Example*: It is possible for a class or struct type `C` to define a conversion from `C` to `int` and from `int` to `C`, but not from `int` to `bool`. *end example*

Expand Down
21 changes: 16 additions & 5 deletions standard/conversions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 10 Conversions

## 10.1 General

Check warning on line 3 in standard/conversions.md

View workflow job for this annotation

GitHub Actions / word-converter

Markdown to Word Converter - MDC032

Line length 91 > maximum 83; Console.WriteLine(identity.Method.ReturnParameter.IsDefined(typeof(MarkAttribute), false));

Check warning on line 4 in standard/conversions.md

View workflow job for this annotation

GitHub Actions / word-converter

Markdown to Word Converter - MDC032

Line length 94 > maximum 83; Console.WriteLine(identity.Method.GetParameters()[0].IsDefined(typeof(MarkAttribute), false));
A ***conversion*** causes an expression to be converted to, or treated as being of, a particular type; in the former case a conversion may involve a change in representation. Conversions can be ***implicit*** or ***explicit***, and this determines whether an explicit cast is required.

Expand Down Expand Up @@ -781,9 +781,14 @@
- If `E` has a type, let `S` be that type.
- If `S` or `T` are nullable value types, let `Sᵢ` and `Tᵢ` be their underlying types, otherwise let `Sᵢ` and `Tᵢ` be `S` and `T`, respectively.
- If `Sᵢ` or `Tᵢ` are type parameters, let `S₀` and `T₀` be their effective base classes, otherwise let `S₀` and `T₀` be `Sᵢ` and `Tᵢ`, respectively.
- Find the set of types, `D`, from which user-defined conversion operators will be considered. This set consists of `S₀` (if `S₀` exists and is a class or struct), the base classes of `S₀` (if `S₀` exists and is a class), and `T₀` (if `T₀` is a class or struct). A type is added to the set `D` only if an identity conversion to another type already included in the set does not exist.

- Find the set of applicable user-defined and lifted conversion operators, `U`. This set consists of the user-defined and lifted implicit conversion operators declared by the classes or structs in `D` that convert from a type encompassing `E` to a type encompassed by `T`. If `U` is empty, the conversion is undefined and a compile-time error occurs.
- Find the set of applicable user-defined and lifted conversion operators, `U`.
- Find the set of types, `D₁`, from which user-defined conversion operators will be considered. This set consists of `S₀` (if `S₀` exists and is a class or struct), the base classes of `S₀` (if `S₀` exists and is a class), and `T₀` (if `T₀` is a class or struct). A type is added to `D₁` only if an identity conversion to another type already included in the set does not exist.
- Find the set of applicable user-defined and lifted conversion operators, `U₁`. This set consists of the user-defined and lifted implicit conversion operators declared by the classes or structs in `D₁` that convert from a type encompassing `E` to a type encompassed by `T`.
- If `U₁` is not empty, then `U` is `U₁`. Otherwise,
- Find the set of types, `D₂`, from which user-defined conversion operators will be considered. This set consists of `Sᵢ` *effective interface set* and their base interfaces (if `Sᵢ` is a type parameter), and `Tᵢ` *effective interface set* (if `Tᵢ` is a type parameter).
- Find the set of applicable user-defined and lifted conversion operators, `U₂`. This set consists of the user-defined and lifted implicit conversion operators declared by the interfaces in `D₂` that convert from a type encompassing `E` to a type encompassed by `T`.
- If `U₂` is not empty, then `U` is `U₂`.
- If `U` is empty, the conversion is undefined and a compile-time error occurs.
- Find the most-specific source type, `Sₓ`, of the operators in `U`:
- If `S` exists and any of the operators in `U` convert from `S`, then `Sₓ` is `S`.
- Otherwise, `Sₓ` is the most-encompassed type in the combined set of source types of the operators in `U`. If exactly one most-encompassed type cannot be found, then the conversion is ambiguous and a compile-time error occurs.
Expand All @@ -809,8 +814,14 @@
- If `E` has a type, let `S` be that type.
- If `S` or `T` are nullable value types, let `Sᵢ` and `Tᵢ` be their underlying types, otherwise let `Sᵢ` and `Tᵢ` be `S` and `T`, respectively.
- If `Sᵢ` or `Tᵢ` are type parameters, let `S₀` and `T₀` be their effective base classes, otherwise let `S₀` and `T₀` be `Sᵢ` and `Tᵢ`, respectively.
- Find the set of types, `D`, from which user-defined conversion operators will be considered. This set consists of `S₀` (if `S₀` exists and is a class or struct), the base classes of `S₀` (if `S₀` exists and is a class), `T₀` (if `T₀` is a class or struct), and the base classes of `T₀` (if `T₀` is a class). A type is added to the set `D` only if an identity conversion to another type already included in the set does not exist.
- Find the set of applicable user-defined and lifted conversion operators, `U`. This set consists of the user-defined and lifted implicit or explicit conversion operators declared by the classes or structs in `D` that convert from a type encompassing `E` or encompassed by `S` (if it exists) to a type encompassing or encompassed by `T`. If `U` is empty, the conversion is undefined and a compile-time error occurs.
- Find the set of applicable user-defined and lifted conversion operators, `U`.
- Find the set of types, `D₁`, from which user-defined conversion operators will be considered. This set consists of `S₀` (if `S₀` exists and is a class or struct), the base classes of `S₀` (if `S₀` exists and is a class), `T₀` (if `T₀` is a class or struct), and the base classes of `T₀` (if `T₀` is a class). A type is added to `D₁` only if an identity conversion to another type already included in the set does not exist.
- Find the set of applicable user-defined and lifted conversion operators, `U₁`. This set consists of the user-defined and lifted implicit or explicit conversion operators declared by the classes or structs in `D₁` that convert from a type encompassing `E` or encompassed by `S` (if it exists) to a type encompassing or encompassed by `T`.
- If `U₁` is not empty, then `U` is `U₁`. Otherwise,
- Find the set of types, `D₂`, from which user-defined conversion operators will be considered. This set consists of `Sᵢ` *effective interface set* and their base interfaces (if `Sᵢ` is a type parameter), and `Tᵢ` *effective interface set* and their base interfaces (if `Tᵢ` is a type parameter).
- Find the set of applicable user-defined and lifted conversion operators, `U₂`. This set consists of the user-defined and lifted implicit or explicit conversion operators declared by the interfaces in `D₂` that convert from a type encompassing `E` or encompassed by `S` (if it exists) to a type encompassing or encompassed by `T`.
- If `U₂` is not empty, then `U` is `U₂`.
- If `U` is empty, the conversion is undefined and a compile-time error occurs.
- Find the most-specific source type, `Sₓ`, of the operators in `U`:
- If `S` exists and any of the operators in `U` convert from `S`, then `Sₓ` is `S`.
- Otherwise, if any of the operators in `U` convert from types that encompass `E`, then `Sₓ` is the most-encompassed type in the combined set of source types of those operators. If no most-encompassed type can be found, then the conversion is ambiguous and a compile-time error occurs.
Expand Down
Loading
Loading