|
15 | 15 | //===----------------------------------------------------------------------===//
|
16 | 16 | // Standardized uninhabited type
|
17 | 17 | //===----------------------------------------------------------------------===//
|
18 |
| -/// The return type of functions that do not return normally, that is, a type |
19 |
| -/// with no values. |
| 18 | +/// A standard _uninhabited_ type---that is, a type which has no values, |
| 19 | +/// and cannot be constructed. |
20 | 20 | ///
|
21 |
| -/// Use `Never` as the return type when declaring a closure, function, or |
22 |
| -/// method that unconditionally throws an error, traps, or otherwise does |
23 |
| -/// not terminate. |
| 21 | +/// Use `Never` as the return type of a _nonreturning_ function (or closure, |
| 22 | +/// method, computed property, or subscript) which unconditionally throws an |
| 23 | +/// error, traps, or otherwise does not terminate. |
24 | 24 | ///
|
| 25 | +/// // An infinite loop will never return. |
| 26 | +/// func forever() -> Never { |
| 27 | +/// while true { |
| 28 | +/// print("I will print forever.") |
| 29 | +/// } |
| 30 | +/// } |
| 31 | +/// |
| 32 | +/// // Calling `fatalError` will unconditionally terminate |
| 33 | +/// // the program. |
25 | 34 | /// func crashAndBurn() -> Never {
|
26 | 35 | /// fatalError("Something very, very bad happened")
|
27 | 36 | /// }
|
| 37 | +/// |
| 38 | +/// As an uninhabited type, `Never` allows you to represent a state in your |
| 39 | +/// program that is impossible to reach during its execution. Swift's type |
| 40 | +/// system can use this information to simplify control statements in cases |
| 41 | +/// known to be unreachable. |
| 42 | +/// |
| 43 | +/// // The `.failure` case can never be reached. |
| 44 | +/// let favoriteNumber: Result<Int, Never> = .success(42) |
| 45 | +/// switch favoriteNumber { |
| 46 | +/// case .success(let value): |
| 47 | +/// print("My favorite number is", value) |
| 48 | +/// } |
| 49 | +/// |
28 | 50 | @frozen
|
29 | 51 | public enum Never {}
|
30 | 52 |
|
|
0 commit comments