swiftlang / swift-book

The Swift Programming Language book
Apache License 2.0
1.76k stars 162 forks source link

Potential Issues In Patterns #272

Open tadbyt opened 9 months ago

tadbyt commented 9 months ago

Location

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/patterns https://docs.swift.org/swift-book/documentation/the-swift-programming-language/patterns#Enumeration-Case-Pattern https://docs.swift.org/swift-book/documentation/the-swift-programming-language/patterns#Type-Casting-Patterns https://docs.swift.org/swift-book/documentation/the-swift-programming-language/patterns#Expression-Pattern

Description

There are two potential issues in the Patterns section. The first is the statement in four locations that certain patterns may only be specified in the case labels of switch statements. Given the parallel syntax and semantics in this area of the switch statement and switch expression, it is unclear whether the references to the switch statement simply weren't updated with the introduction of the conditional expressions or whether the limitation still holds true.

The second issue appears in the second sentence of the following paragraph:

The as pattern matches a value if the type of that value at runtime is the same as the type specified in the right-hand side of the as pattern — or a subclass of that type. If the match succeeds, the type of the matched value is cast to the pattern specified in the right-hand side of the as pattern.

Given the syntax of an as pattern, it appears that it should read:

If the match succeeds, the type of the matched value is cast to the pattern specified in the left-hand side of the as pattern.

Correction

Either the following references should be updated to explicitly include switch expressions OR in each case, a brief statement reinforcing the limitation to switch statements and not switch expressions should be given.

Option 1:

(End of the introduction of the Patterns subsection) You use these patterns in a case label of a switch statement, a case label of a switch expression, a catch clause of a do statement, or in the case condition of an if, while, guard, or for-in statement.

(Second sentence of the first paragraph of the Enumeration Case Pattern subsection) Enumeration case patterns appear in switch statement case labels, switch expression case labels, and in the case conditions of if, while, guard, and for-in statements.

(Second sentence of the first paragraph of the Type Casting Patterns subsection) The is pattern appears only in switch statement and switch expression case labels.

(Second sentence of the first paragraph of the Expression Patterns subsection) Expression patterns appear only in switch statement and switch expression case labels.

Option 2:

(End of the introduction of the Patterns subsection) You use these patterns in a case label of a switch statement, a catch clause of a do statement, or in the case condition of an if, while, guard, or for-in statement. These patterns may not be given in the case label of a switch expression.

(Second sentence of the first paragraph of the Enumeration Case Pattern subsection) Enumeration case patterns appear in switch statement case labels and in the case conditions of if, while, guard, and for-in statements. An enumeration case pattern may not be given in the case label of a switch expression.

(Second sentence of the first paragraph of the Type Casting Patterns subsection) The is pattern appears only in switch statement case labels. A type casting pattern may not be given in the case label of a switch expression.

(Second sentence of the first paragraph of the Expression Patterns subsection) Expression patterns appear only in switch statement case labels. An expression pattern may not be given in the case label of a switch expression.

For the second issue, change the second sentence in the third paragraph of the Type Casting Patterns subsection to:

If the match succeeds, the type of the matched value is cast to the pattern specified in the left-hand side of the as pattern.