10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- /// A value that represents either a success or failure, capturing associated
14
- /// values in both cases .
13
+ /// A value that represents either a success or a failure, including an
14
+ /// associated value in each case .
15
15
@_frozen
16
16
public enum Result < Success, Failure: Error > {
17
17
/// A success, storing a `Success` value.
@@ -20,13 +20,24 @@ public enum Result<Success, Failure: Error> {
20
20
/// A failure, storing a `Failure` value.
21
21
case failure( Failure )
22
22
23
- /// Evaluates the given closure when this `Result` instance is `.success`,
24
- /// passing the success value as a parameter .
23
+ /// Returns a new result, mapping any success value using the given
24
+ /// transformation .
25
25
///
26
- /// - Parameter transform: A closure that takes the success value of the
26
+ /// Use this method when you need to transform the value of a `Result`
27
+ /// instance when it represents a success. The following example transforms
28
+ /// the integer success value of a result into a string:
29
+ ///
30
+ /// func getNextInteger() -> Result<Int, Error> { ... }
31
+ ///
32
+ /// let integerResult = getNextInteger()
33
+ /// // integerResult == .success(5)
34
+ /// let stringResult = integerResult.map({ String($0) })
35
+ /// // stringResult == .success("5")
36
+ ///
37
+ /// - Parameter transform: A closure that takes the success value of this
27
38
/// instance.
28
- /// - Returns: A `Result` instance with the result of evaluating the given
29
- /// closure as the new success value if this instance is `. success` .
39
+ /// - Returns: A `Result` instance with the result of evaluating `transform`
40
+ /// as the new success value if this instance represents a success.
30
41
public func map< NewSuccess> (
31
42
_ transform: ( Success ) -> NewSuccess
32
43
) -> Result < NewSuccess , Failure > {
@@ -38,13 +49,32 @@ public enum Result<Success, Failure: Error> {
38
49
}
39
50
}
40
51
41
- /// Evaluates the given closure when this `Result` instance is `.failure`,
42
- /// passing the failure value as a parameter.
52
+ /// Returns a new result, mapping any failure value using the given
53
+ /// transformation.
54
+ ///
55
+ /// Use this method when you need to transform the value of a `Result`
56
+ /// instance when it represents a failure. The following example transforms
57
+ /// the error value of a result by wrapping it in a custom `Error` type:
58
+ ///
59
+ /// struct DatedError: Error {
60
+ /// var error: Error
61
+ /// var date: Date
62
+ ///
63
+ /// init(_ error: Error) {
64
+ /// self.error = error
65
+ /// self.date = Date()
66
+ /// }
67
+ /// }
68
+ ///
69
+ /// let result: Result<Int, Error> = ...
70
+ /// // result == .failure(<error value>)
71
+ /// let resultWithDatedError = result.mapError({ e in DatedError(e) })
72
+ /// // result == .failure(DatedError(error: <error value>, date: <date>))
43
73
///
44
74
/// - Parameter transform: A closure that takes the failure value of the
45
75
/// instance.
46
- /// - Returns: A `Result` instance with the result of evaluating the given
47
- /// closure as the new failure value if this instance is `. failure` .
76
+ /// - Returns: A `Result` instance with the result of evaluating `transform`
77
+ /// as the new failure value if this instance represents a failure.
48
78
public func mapError< NewFailure> (
49
79
_ transform: ( Failure ) -> NewFailure
50
80
) -> Result < Success , NewFailure > {
@@ -56,13 +86,13 @@ public enum Result<Success, Failure: Error> {
56
86
}
57
87
}
58
88
59
- /// Evaluates the given closure when this `Result` instance is `.success`,
60
- /// passing the success value as a parameter .
89
+ /// Returns a new result, mapping any success value using the given
90
+ /// transformation and unwrapping the produced result .
61
91
///
62
92
/// - Parameter transform: A closure that takes the success value of the
63
93
/// instance.
64
- /// - Returns: A`Result` instance, either from the closure or the previous
65
- /// `. failure` .
94
+ /// - Returns: A `Result` instance with the result of evaluating `transform`
95
+ /// as the new failure value if this instance represents a failure .
66
96
public func flatMap< NewSuccess> (
67
97
_ transform: ( Success ) -> Result < NewSuccess , Failure >
68
98
) -> Result < NewSuccess , Failure > {
@@ -74,8 +104,8 @@ public enum Result<Success, Failure: Error> {
74
104
}
75
105
}
76
106
77
- /// Evaluates the given closure when this `Result` instance is `.failure`,
78
- /// passing the failure value as a parameter .
107
+ /// Returns a new result, mapping any failure value using the given
108
+ /// transformation and unwrapping the produced result .
79
109
///
80
110
/// - Parameter transform: A closure that takes the failure value of the
81
111
/// instance.
@@ -92,10 +122,22 @@ public enum Result<Success, Failure: Error> {
92
122
}
93
123
}
94
124
95
- /// Get the success value as a throwing expression.
125
+ /// Returns the success value as a throwing expression.
126
+ ///
127
+ /// Use this method to retrieve the value of this result if it represents a
128
+ /// success, or to catch the value if it represents a failure.
129
+ ///
130
+ /// let integerResult: Result<Int, Error> = .success(5)
131
+ /// do {
132
+ /// let value = try integerResult.get()
133
+ /// print("The value is \(value).")
134
+ /// } catch error {
135
+ /// print("Error retrieving the value: \(error)")
136
+ /// }
137
+ /// // Prints "The value is 5."
96
138
///
97
- /// - Returns: The success value, if the instance is `. success` .
98
- /// - Throws: The failure value, if the instance is `. failure` .
139
+ /// - Returns: The success value, if the instance represent a success.
140
+ /// - Throws: The failure value, if the instance represents a failure.
99
141
public func get( ) throws -> Success {
100
142
switch self {
101
143
case let . success( success) :
@@ -107,10 +149,10 @@ public enum Result<Success, Failure: Error> {
107
149
}
108
150
109
151
extension Result where Failure == Swift . Error {
110
- /// Create an instance by evaluating a throwing closure, capturing the
111
- /// returned value as a `. success` or any thrown error as `. failure` .
152
+ /// Creates a new result by evaluating a throwing closure, capturing the
153
+ /// returned value as a success, or any thrown error as a failure.
112
154
///
113
- /// - Parameter catching : A throwing closure to evaluate.
155
+ /// - Parameter body : A throwing closure to evaluate.
114
156
@_transparent
115
157
public init ( catching body: ( ) throws -> Success ) {
116
158
do {
0 commit comments