Skip to content

Commit 2bb0b5c

Browse files
committed
Revise some documentation comments.
1 parent b167703 commit 2bb0b5c

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

stdlib/public/core/Result.swift

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

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.
1515
@_frozen
1616
public enum Result<Success, Failure: Error> {
1717
/// A success, storing a `Success` value.
@@ -20,13 +20,24 @@ public enum Result<Success, Failure: Error> {
2020
/// A failure, storing a `Failure` value.
2121
case failure(Failure)
2222

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.
2525
///
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
2738
/// 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.
3041
public func map<NewSuccess>(
3142
_ transform: (Success) -> NewSuccess
3243
) -> Result<NewSuccess, Failure> {
@@ -38,13 +49,32 @@ public enum Result<Success, Failure: Error> {
3849
}
3950
}
4051

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>))
4373
///
4474
/// - Parameter transform: A closure that takes the failure value of the
4575
/// 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.
4878
public func mapError<NewFailure>(
4979
_ transform: (Failure) -> NewFailure
5080
) -> Result<Success, NewFailure> {
@@ -56,13 +86,13 @@ public enum Result<Success, Failure: Error> {
5686
}
5787
}
5888

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.
6191
///
6292
/// - Parameter transform: A closure that takes the success value of the
6393
/// 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.
6696
public func flatMap<NewSuccess>(
6797
_ transform: (Success) -> Result<NewSuccess, Failure>
6898
) -> Result<NewSuccess, Failure> {
@@ -74,8 +104,8 @@ public enum Result<Success, Failure: Error> {
74104
}
75105
}
76106

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.
79109
///
80110
/// - Parameter transform: A closure that takes the failure value of the
81111
/// instance.
@@ -92,10 +122,22 @@ public enum Result<Success, Failure: Error> {
92122
}
93123
}
94124

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."
96138
///
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.
99141
public func get() throws -> Success {
100142
switch self {
101143
case let .success(success):
@@ -107,10 +149,10 @@ public enum Result<Success, Failure: Error> {
107149
}
108150

109151
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.
112154
///
113-
/// - Parameter catching: A throwing closure to evaluate.
155+
/// - Parameter body: A throwing closure to evaluate.
114156
@_transparent
115157
public init(catching body: () throws -> Success) {
116158
do {

0 commit comments

Comments
 (0)