|
15 | 15 | //===----------------------------------------------------------------------===//
|
16 | 16 | // Standardized uninhabited type
|
17 | 17 | //===----------------------------------------------------------------------===//
|
18 |
| -/// A standard _uninhabited_ type---that is, a type which has no values, |
19 |
| -/// and cannot be constructed. |
| 18 | +/// A type that has no values and can't be constructed. |
20 | 19 | ///
|
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. |
| 20 | +/// Use `Never` as the return type of a function |
| 21 | +/// that doesn't ever return normally --- for example, |
| 22 | +/// because it runs forever or terminates the program. |
24 | 23 | ///
|
25 |
| -/// // An infinite loop will never return. |
| 24 | +/// // An infinite loop never returns. |
26 | 25 | /// func forever() -> Never {
|
27 | 26 | /// while true {
|
28 | 27 | /// print("I will print forever.")
|
29 | 28 | /// }
|
30 | 29 | /// }
|
31 | 30 | ///
|
32 |
| -/// // Calling `fatalError` will unconditionally terminate |
33 |
| -/// // the program. |
| 31 | +/// // Calling fatalError(_file:line:) unconditionally stops the program. |
34 | 32 | /// func crashAndBurn() -> Never {
|
35 | 33 | /// fatalError("Something very, very bad happened")
|
36 | 34 | /// }
|
37 | 35 | ///
|
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. |
| 36 | +/// A function that returns `Never` is called a _nonreturning_ function. |
| 37 | +/// Closures, methods, computed properties, and subscripts |
| 38 | +/// can also be nonreturning. |
| 39 | +/// |
| 40 | +/// There's no way to create an instance of `Never`; |
| 41 | +/// this characteristic makes it an _uninhabited_ type. |
| 42 | +/// You can use an uninhabited type like `Never` |
| 43 | +/// to represent states in your program |
| 44 | +/// that are impossible to reach during execution. |
| 45 | +/// Swift's type system uses this information --- |
| 46 | +/// for example, to reason about control statements |
| 47 | +/// in cases that are known to be unreachable. |
42 | 48 | ///
|
43 |
| -/// // The `.failure` case can never be reached. |
44 | 49 | /// let favoriteNumber: Result<Int, Never> = .success(42)
|
45 | 50 | /// switch favoriteNumber {
|
46 | 51 | /// case .success(let value):
|
47 | 52 | /// print("My favorite number is", value)
|
48 | 53 | /// }
|
49 | 54 | ///
|
| 55 | +/// In the code above, |
| 56 | +/// the result type has a failure type of `Never`, |
| 57 | +/// indicating that it always succeeds. |
| 58 | +/// The switch statement is therefore exhaustive, |
| 59 | +/// even though it doesn't contain a `.failure` case, |
| 60 | +/// because that case could never be reached. |
50 | 61 | @frozen
|
51 | 62 | public enum Never {}
|
52 | 63 |
|
|
0 commit comments