swift-book icon indicating copy to clipboard operation
swift-book copied to clipboard

Eroneous Categorization of Compound Assignment Operator in Formal Grammar

Open tadbyt opened this issue 1 year ago • 0 comments

Location

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators#Compound-Assignment-Operators https://docs.swift.org/swift-book/documentation/the-swift-programming-language/advancedoperators#Compound-Assignment-Operators https://docs.swift.org/swift-book/documentation/the-swift-programming-language/expressions#Infix-Expressions https://docs.swift.org/swift-book/documentation/the-swift-programming-language/expressions#Assignment-Operator https://docs.swift.org/swift-book/documentation/the-swift-programming-language/summaryofthegrammar#Expressions https://developer.apple.com/documentation/swift/operator-declarations

Description

According to the formal grammar, compound assignment operators should be considered as infix rather than assignment operators as the Grammar of an assignment operator only has a production yielding =. It could be argued that compound assignment operators should be treated as assignment operators rather than infix operators as they treat the left side operand as a target as well as a source, requiring an update to the Assignment Operator subsection. Alternatively, there could be another subsection added for Compound Assignment Operators that recognizes that the behavior of the left operand is a hybrid of both an infix operator and assignment operator.

Note: Xcode allows both await and try after a compound assignment operator.

Regardless of which approach is taken, some guidance should be provided with respect to await and throws with respect to a compound assignment operator, in particular, do they just apply to the evaluation of the right operand or to the compound assignment operation itself, to the prior evaluations of the expression, or some combination.

Correction

Option 1: Add a second production or alternative to the assignment operator grammar for a compound assignment operator and update the Assignment Operator subsection to read:

Assignment Operator

The assignment operator sets a new value for a given expression. It has the following forms:

<#expression#> = <#value#> <#expression#> <#compound-assignment-operator#> <#value#>

In the first form, the value of the expression is set to the value obtained by evaluating the value. If the expression is a tuple, the value must be a tuple with the same number of elements. (Nested tuples are allowed.) Assignment is performed from each part of the value to the corresponding part of the expression. For example:

(a, _, (b, c)) = ("test", 9.45, (12, 3)) // a is "test", b is 12, c is 3, and 9.45 is ignored

In the second form, there are two cases, those compound assignment operator implemented in the Swift standard library and those implemented by a custom function.

For those implemented in the Swift standard library, there are two conventions that are followed. First, the compound assignment operator consists of = appended to a binary infix operator. Second, expression op= value is functionally equivalent to expression = expression op value. For more information, see Compound Assignment Operators (in Language Guide > Basic Operators) and Operator Declarations.

For those custom implemented, both conventions should be followed, but are not required. For more information, see Compound Assignment Operators (in Language Guide > Advanced Operators).

The assignment operator doesn’t return any value.

Grammar of an assignment operator

assignment-operator → = assignment-operator → Compound assignment operator

Option 2: Add an addition production to the Grammar of an infix expression:

infix-expression → compound-assignment-operator try-operator? await-operator? prefix-expression

Add a new subsection for Compound Assignment Operators concluded with a new Grammar of a compound assignment operator as follows:

Compound Assignment Operator

The compound assignment operator performs a binary operation on the expression and value and then sets the resultant value to the expression. It has the following form:

<#expression#> <#compound-assignment-operator#> <#value#>

There are two cases, those compound assignment operator implemented in the Swift standard library and those implemented by a custom function.

For those implemented in the Swift standard library, there are two conventions that are followed. First, the compound assignment operator consists of = appended to a binary infix operator. Second, expression op= value is functionally equivalent to expression = expression op value. For more information, see Compound Assignment Operators (in Language Guide > Basic Operators) and Operator Declarations.

For those custom implemented, both conventions should be followed, but are not required. For more information, see Compound Assignment Operators (in Language Guide > Advanced Operators).

The compound assignment operator doesn’t return any value.

Grammar of a compound assignment operator:

compound-assignment-operator → Compound assignment operator

tadbyt avatar Jan 26 '24 03:01 tadbyt