Skip to content

Commit cf9fcb9

Browse files
author
eunjiChung
committed
[stdlib] Add flatMap example in Result.swift
1 parent 50c0534 commit cf9fcb9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

stdlib/public/core/Result.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ 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 when you need to transform the value of a `Result`
93+
/// instance eventhough it produces a nested 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+
/// func getNextAfterInteger() -> Result<Int, Error> { /* ... */ }
100+
///
101+
/// let result = getNextInteger().map({ getNextAfterInteger($0) })
102+
/// // result == .success(.success(5))
103+
///
104+
/// let result = getNextInteger().flatMap({ getNextAfterInteger($0) })
105+
/// // result == .success(5)
106+
///
92107
/// - Parameter transform: A closure that takes the success value of the
93108
/// instance.
94109
/// - Returns: A `Result` instance with the result of evaluating `transform`

0 commit comments

Comments
 (0)