Skip to content

Commit

Permalink
Updated & extended spec for null-forgiving expressions:
Browse files Browse the repository at this point in the history
- Added null conditional support
- Distinguished uses of logical negation, `!x`, from null-forgiving, `x!`,
throughout the Standard. Null-forgiving is a non-overloadable psuedo op.
- Added placeholders in places where what the spec is has yet to be decided as the details were not obtained from the LDM notes or other design documents but from the source of one particular compiler. These will be replaced in a PR review posted immediately after this commit, each placeholder will have at least to suggested options to vote on.
- Some typos were fixed or wording changes made, some in areas not directly related to null-forgiving, as they were found during work on this PR.
  • Loading branch information
Nigel-Ecma committed Nov 9, 2024
1 parent dcd7eed commit 6b8ec80
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 41 deletions.
10 changes: 8 additions & 2 deletions standard/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4453,8 +4453,12 @@ unary_operator_declarator
: type 'operator' overloadable_unary_operator '(' fixed_parameter ')'
;
logical_negation_operator
: '!'
;
overloadable_unary_operator
: '+' | '-' | '!' | '~' | '++' | '--' | 'true' | 'false'
: '+' | '-' | logical_negation_operator | '~' | '++' | '--' | 'true' | 'false'
;
binary_operator_declarator
Expand All @@ -4481,6 +4485,8 @@ operator_body
*unsafe_modifier* ([§23.2](unsafe-code.md#232-unsafe-contexts)) is only available in unsafe code ([§23](unsafe-code.md#23-unsafe-code)).

*Note*: The prefix logical negation ([§12.9.4](expressions.md#1294-logical-negation-operator)) and postfix null-forgiving operators ([§12.8.9](expressions.md#1289-null-forgiving-expressions)), while represented by the same lexical token (`!`), are distinct. The latter is not an overloadable operator. *end note*

There are three categories of overloadable operators: Unary operators ([§15.10.2](classes.md#15102-unary-operators)), binary operators ([§15.10.3](classes.md#15103-binary-operators)), and conversion operators ([§15.10.4](classes.md#15104-conversion-operators)).

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.
Expand All @@ -4507,7 +4513,7 @@ Additional information on conversion operators can be found in [§10.5](convers

The following rules apply to unary operator declarations, where `T` denotes the instance type of the class or struct that contains the operator declaration:

- A unary `+`, `-`, `!`, or `~` operator shall take a single parameter of type `T` or `T?` and can return any type.
- A unary `+`, `-`, `!` (logical negation only), or `~` operator shall take a single parameter of type `T` or `T?` and can return any type.
- A unary `++` or `--` operator shall take a single parameter of type `T` or `T?` and shall return that same type or a type derived from it.
- A unary `true` or `false` operator shall take a single parameter of type `T` or `T?` and shall return type `bool`.

Expand Down
Loading

0 comments on commit 6b8ec80

Please sign in to comment.