You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/0099-conditionclauses.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@
8
8
9
9
## Introduction
10
10
11
-
Swift condition clauses appear in `guard`, `if`, and `while` statements. This proposal re-architects the condition grammar to enable an arbitrary mix of Boolean expressions, `let` conditions (which test and unwrap optionals), general `case` clauses for arbitrary pattern matching, and availability tests. It removes `where` clauses from optional binding conditions and case conditions, and eliminates gramatical ambiguity by using commas for separation between clauses instead of using them both to separate clauses and terms within each clause. This eliminates ambiguity problems in the current syntax, and alleviates the situation where many Swift developers don't know they can use arbitrary Boolean conditions after a value binding.
11
+
Swift condition clauses appear in `guard`, `if`, and `while` statements. This proposal re-architects the condition grammar to enable an arbitrary mix of Boolean expressions, `let` conditions (which test and unwrap optionals), general `case` clauses for arbitrary pattern matching, and availability tests. It removes `where` clauses from optional binding conditions and case conditions, and eliminates gramatical ambiguity by using commas for separation between clauses instead of using them both to separate clauses and terms within each clause. These modifications streamline Swift's syntax and alleviate the situation where many Swift developers don't know they can use arbitrary Boolean conditions after a value binding.
12
12
13
13
Swift-evolution thread:
14
14
[\[Pitch\] making where and , interchangeable in guard conditions](http://thread.gmane.org/gmane.comp.lang.swift.evolution/17926)
The rules are complex, confusing, and imprecise. These rules establish that standalone Boolean tests must either precede binding or be joined with optional binding and pattern conditions. The `where` clause, which makes sense in for loops and switch statement pattern matching, adds little to optional binding in condition clauses, as in the following example. It allows for code such as:
30
+
The rules are complex, confusing, and imprecise. The grammar establishes that standalone Boolean tests must either precede binding or be joined with optional binding and pattern conditions. The `where` clause, which makes sense in for loops and switch statement pattern matching, adds little to optional binding in condition clauses, as in the following example. It allows for code such as:
31
31
32
32
```swift
33
33
guard
@@ -38,7 +38,7 @@ guard
38
38
39
39
In this example, the Boolean `z ==2` clause has no semantic relationship to the optional condition to which it's syntactically bound. Eliminating `where` enables the subordinate condition to stand on its own and be treated as a first classtest among peers.
40
40
41
-
The root problem lies in the condition grammar: commas are used both to separate items within a clause (e.g. in `if let x = a, y = b {`) and to separate mixed kinds ofclauses (e.g. `iflet x = a, case y?= b {`). This proposal resolves this problem by retaining commas as separators between clauses (as used elsewhere in Swift) and eliminating the ability have multiple items within a clause.
41
+
The root problem lies in the condition grammar: commas are used both to separate items within a clause (e.g. in `if let x = a, y = b {`) and to separate mixed kinds ofclauses (e.g. `iflet x = a, case y?= b {`). This proposal resolves this problem by retaining commas as separators between clauses (as used elsewhere in Swift) and limits clauses to single items.
42
42
43
43
After adoption of these changes, the previous example would be written inany of
44
44
these styles:
@@ -61,15 +61,15 @@ This approach also solves ambiguity problems with the current grammar. For examp
61
61
ifcaselet x = a, let y = b {
62
62
```
63
63
64
-
With the new approach, this is unambiguously an `ifcase` followed by an `iflet`. To get two `ifcase` clauses, it would be written as:
64
+
With the new approach, this is unambiguously an `ifcase` followed by an `iflet`. To include two `ifcase` clauses, repeat the `case` keyword:
65
65
66
66
```swift
67
67
ifcaselet x = a, caselet y = b {
68
68
```
69
69
70
70
The advantages in accepting this proposal are:
71
71
72
-
* The "list of lists" ambiguity problems are solved, and uses a cleaner and simpler grammar.
72
+
* The "list of lists" ambiguity problems are solved. Swift uses a cleaner and simpler grammar.
73
73
* `where` clauses are no longer used to conjoin Boolean expressions with conditional binding. This fixes user confusion issues and addresses a problem where Boolean conditions need to be attached to arbitrary bindings.
0 commit comments