Skip to content

Commit db2ade0

Browse files
committed
Outline discussion of if-case
1 parent bc4a629 commit db2ade0

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

TSPL.docc/LanguageGuide/ControlFlow.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,6 @@ on the right-hand side of an assignment,
880880
as shown in the examples above,
881881
you can also use them as the value that a function or closure returns.
882882

883-
<!-- XXX forward reference to if-case syntax? -->
884-
885883
### Switch
886884

887885
A `switch` statement considers a value
@@ -1298,8 +1296,6 @@ Because `anotherPoint` is always a tuple of two values,
12981296
this case matches all possible remaining values,
12991297
and a `default` case isn't needed to make the `switch` statement exhaustive.
13001298

1301-
<!-- XXX cross reference if-case syntax from here -->
1302-
13031299
#### Where
13041300

13051301
A `switch` case can use a `where` clause to check for additional conditions.
@@ -1444,6 +1440,35 @@ and `distance` is an integer in both patterns ---
14441440
which means that the code in the body of the `case`
14451441
can always access a value for `distance`.
14461442

1443+
## Patterns
1444+
1445+
XXX OUTLINE:
1446+
1447+
- Each switch case takes a *pattern*
1448+
that describes what values that case handles
1449+
- You can also use patterns with if and for
1450+
- The for-case syntax is useful when you want to iterate
1451+
over only certain elements in a collection
1452+
- Because patterns can bind values,
1453+
they're also useful for working with enumerations that have associated values
1454+
XREF <doc:Enumerations#Associated-Values>
1455+
1456+
```swift
1457+
let somePoint = (12, 100)
1458+
if case (let x, 100) = somePoint {
1459+
print("Found a point with x of \(x)")
1460+
}
1461+
1462+
let points = [(10, 0), (30, 100), (-20, 0)]
1463+
for case (let x, let y) in points {
1464+
guard y == 0 else { continue }
1465+
print("Found a point on the x-axis at \(x)")
1466+
}
1467+
for case (let x, let y) in points where y == 0 {
1468+
print("Found a point on the x-axis at \(x)")
1469+
}
1470+
```
1471+
14471472
## Control Transfer Statements
14481473

14491474
*Control transfer statements* change the order in which your code is executed,

0 commit comments

Comments
 (0)