Skip to content

Commit a16329f

Browse files
committed
Improve Never documentation
1 parent 1f3e159 commit a16329f

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

stdlib/public/core/Policy.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,38 @@
1515
//===----------------------------------------------------------------------===//
1616
// Standardized uninhabited type
1717
//===----------------------------------------------------------------------===//
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.
2020
///
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.
2424
///
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.
2534
/// func crashAndBurn() -> Never {
2635
/// fatalError("Something very, very bad happened")
2736
/// }
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+
///
2850
@frozen
2951
public enum Never {}
3052

0 commit comments

Comments
 (0)