Skip to content

Commit ffe471a

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

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

stdlib/public/core/Result.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,18 @@ 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.
92+
/// Use this method to avoid a nested result when your transformation
93+
/// produces another `Result` type.
9494
///
9595
/// In this example, note the difference in the result of using `map` and
9696
/// `flatMap` with a transformation that returns an result type.
9797
///
98-
/// func getNextInteger() -> Result<Int, Error> { /* ... */ }
99-
/// func getNextAfterInteger() -> Result<Int, Error> { /* ... */ }
98+
/// func getNextInteger() -> Result<Int, Error> {
99+
/// .success(4)
100+
/// }
101+
/// func getNextAfterInteger(_ n: Int) -> Result<Int, Error> {
102+
/// .success(n + 1)
103+
/// }
100104
///
101105
/// let result = getNextInteger().map({ getNextAfterInteger($0) })
102106
/// // result == .success(.success(5))

0 commit comments

Comments
 (0)