Skip to content

Commit 779a410

Browse files
committed
Revise docs for Never for reference style.
- Reduce the emphasis on the type theory that Never is an uninhabited type, focusing more on its meaning and usage in code. - Move the definition of uninhabited out of the abstract. Define "nonreturning" more explicitly. - Expand the favoriteNumber example's code comment into a brief paragraph to walk through the code listing. - Avoid italics in the abstract, future tense, and parenthetical asides. - Use contractions.
1 parent a16329f commit 779a410

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

stdlib/public/core/Policy.swift

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,49 @@
1515
//===----------------------------------------------------------------------===//
1616
// Standardized uninhabited type
1717
//===----------------------------------------------------------------------===//
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.
2019
///
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.
2423
///
25-
/// // An infinite loop will never return.
24+
/// // An infinite loop never returns.
2625
/// func forever() -> Never {
2726
/// while true {
2827
/// print("I will print forever.")
2928
/// }
3029
/// }
3130
///
32-
/// // Calling `fatalError` will unconditionally terminate
33-
/// // the program.
31+
/// // Calling fatalError(_file:line:) unconditionally stops the program.
3432
/// func crashAndBurn() -> Never {
3533
/// fatalError("Something very, very bad happened")
3634
/// }
3735
///
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.
4248
///
43-
/// // The `.failure` case can never be reached.
4449
/// let favoriteNumber: Result<Int, Never> = .success(42)
4550
/// switch favoriteNumber {
4651
/// case .success(let value):
4752
/// print("My favorite number is", value)
4853
/// }
4954
///
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.
5061
@frozen
5162
public enum Never {}
5263

0 commit comments

Comments
 (0)