Skip to content

Commit 66311e1

Browse files
authored
Merge pull request #32115 from eunjiChung/example-result
2 parents 5311021 + ffe471a commit 66311e1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

stdlib/public/core/Result.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ public enum Result<Success, Failure: Error> {
8989
/// Returns a new result, mapping any success value using the given
9090
/// transformation and unwrapping the produced result.
9191
///
92+
/// Use this method to avoid a nested result when your transformation
93+
/// produces another `Result` type.
94+
///
95+
/// In this example, note the difference in the result of using `map` and
96+
/// `flatMap` with a transformation that returns an result type.
97+
///
98+
/// func getNextInteger() -> Result<Int, Error> {
99+
/// .success(4)
100+
/// }
101+
/// func getNextAfterInteger(_ n: Int) -> Result<Int, Error> {
102+
/// .success(n + 1)
103+
/// }
104+
///
105+
/// let result = getNextInteger().map({ getNextAfterInteger($0) })
106+
/// // result == .success(.success(5))
107+
///
108+
/// let result = getNextInteger().flatMap({ getNextAfterInteger($0) })
109+
/// // result == .success(5)
110+
///
92111
/// - Parameter transform: A closure that takes the success value of the
93112
/// instance.
94113
/// - Returns: A `Result` instance with the result of evaluating `transform`

0 commit comments

Comments
 (0)