Skip to content

Commit 13ffd62

Browse files
committed
Show if-case syntax for enums
1 parent 378f76a commit 13ffd62

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

TSPL.docc/LanguageGuide/ControlFlow.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,8 @@ 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+
883885
### Switch
884886

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

1301+
<!-- XXX cross reference if-case syntax from here -->
1302+
12991303
#### Where
13001304

13011305
A `switch` case can use a `where` clause to check for additional conditions.

TSPL.docc/LanguageGuide/Enumerations.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,31 @@ case let .qrCode(productCode):
479479
```
480480
-->
481481

482+
When you're matching just one case of an enumeration,
483+
you can use the shorter `if case` syntax
484+
instead of writing a full switch statement.
485+
For example:
486+
487+
```swift
488+
if case .qrCode(let productCode) = productBarcode {
489+
print("QR code: \(productCode).")
490+
}
491+
```
492+
493+
In this code,
494+
the condition for the `if` statement starts with `case`,
495+
indicating that the condition is a pattern instead of a Boolean value.
496+
If the pattern matches,
497+
the condition for the `if` is considered to be true;
498+
otherwise it's false.
499+
Just like in the switch statement earlier,
500+
the `productBarcode` variable is matched against
501+
the pattern `.qrCode(let productCode)` here.
502+
And like in the switch case,
503+
writing `let` extracts the associated value as a constant.
504+
505+
<!-- XXX mention guards, and maybe for loops -->
506+
482507
## Raw Values
483508

484509
The barcode example in <doc:Enumerations#Associated-Values>
@@ -520,6 +545,8 @@ Raw values can be
520545
strings, characters, or any of the integer or floating-point number types.
521546
Each raw value must be unique within its enumeration declaration.
522547

548+
<!-- XXX fix this note box abuse; this is important, not optional -->
549+
523550
> Note: Raw values are *not* the same as associated values.
524551
> Raw values are set to prepopulated values
525552
> when you first define the enumeration in your code,

0 commit comments

Comments
 (0)