13
13
/// A value that represents either a success or failure, capturing associated
14
14
/// values in both cases.
15
15
@_frozen
16
- public enum Result < Value , Error : Swift . Error > {
17
- /// A success, storing a `Value` .
18
- case value ( Value )
16
+ public enum Result < Success , Failure : Error > {
17
+ /// A success, storing a `Success` value .
18
+ case success ( Success )
19
19
20
- /// A failure, storing an `Error` .
21
- case error ( Error )
20
+ /// A failure, storing a `Failure` value .
21
+ case failure ( Failure )
22
22
23
23
/// Evaluates the given transform closure when this `Result` instance is
24
- /// `.value `, passing the value as a parameter.
24
+ /// `.success `, passing the value as a parameter.
25
25
///
26
26
/// Use the `map` method with a closure that returns a non-`Result` value.
27
27
///
28
28
/// - Parameter transform: A closure that takes the successful value of the
29
29
/// instance.
30
30
/// - Returns: A new `Result` instance with the result of the transform, if
31
31
/// it was applied.
32
- public func map< NewValue > (
33
- _ transform: ( Value ) -> NewValue
34
- ) -> Result < NewValue , Error > {
32
+ public func map< NewSuccess > (
33
+ _ transform: ( Success ) -> NewSuccess
34
+ ) -> Result < NewSuccess , Failure > {
35
35
switch self {
36
- case let . value ( value ) :
37
- return . value ( transform ( value ) )
38
- case let . error ( error ) :
39
- return . error ( error )
36
+ case let . success ( success ) :
37
+ return . success ( transform ( success ) )
38
+ case let . failure ( failure ) :
39
+ return . failure ( failure )
40
40
}
41
41
}
42
42
43
43
/// Evaluates the given transform closure when this `Result` instance is
44
- /// `.error `, passing the error as a parameter.
44
+ /// `.failure `, passing the error as a parameter.
45
45
///
46
46
/// Use the `mapError` method with a closure that returns a non-`Result`
47
47
/// value.
@@ -50,82 +50,82 @@ public enum Result<Value, Error: Swift.Error> {
50
50
/// instance.
51
51
/// - Returns: A new `Result` instance with the result of the transform, if
52
52
/// it was applied.
53
- public func mapError< NewError > (
54
- _ transform: ( Error ) -> NewError
55
- ) -> Result < Value , NewError > {
53
+ public func mapError< NewFailure > (
54
+ _ transform: ( Failure ) -> NewFailure
55
+ ) -> Result < Success , NewFailure > {
56
56
switch self {
57
- case let . value ( value ) :
58
- return . value ( value )
59
- case let . error ( error ) :
60
- return . error ( transform ( error ) )
57
+ case let . success ( success ) :
58
+ return . success ( success )
59
+ case let . failure ( failure ) :
60
+ return . failure ( transform ( failure ) )
61
61
}
62
62
}
63
63
64
64
/// Evaluates the given transform closure when this `Result` instance is
65
- /// `.value `, passing the value as a parameter and flattening the result.
65
+ /// `.success `, passing the value as a parameter and flattening the result.
66
66
///
67
67
/// - Parameter transform: A closure that takes the successful value of the
68
68
/// instance.
69
69
/// - Returns: A new `Result` instance, either from the transform or from
70
70
/// the previous error value.
71
- public func flatMap< NewValue > (
72
- _ transform: ( Value ) -> Result < NewValue , Error >
73
- ) -> Result < NewValue , Error > {
71
+ public func flatMap< NewSuccess > (
72
+ _ transform: ( Success ) -> Result < NewSuccess , Failure >
73
+ ) -> Result < NewSuccess , Failure > {
74
74
switch self {
75
- case let . value ( value ) :
76
- return transform ( value )
77
- case let . error ( error ) :
78
- return . error ( error )
75
+ case let . success ( success ) :
76
+ return transform ( success )
77
+ case let . failure ( failure ) :
78
+ return . failure ( failure )
79
79
}
80
80
}
81
81
82
82
/// Evaluates the given transform closure when this `Result` instance is
83
- /// `.error `, passing the error as a parameter and flattening the result.
83
+ /// `.failure `, passing the error as a parameter and flattening the result.
84
84
///
85
85
/// - Parameter transform: A closure that takes the error value of the
86
86
/// instance.
87
87
/// - Returns: A new `Result` instance, either from the transform or from
88
88
/// the previous success value.
89
- public func flatMapError< NewError > (
90
- _ transform: ( Error ) -> Result < Value , NewError >
91
- ) -> Result < Value , NewError > {
89
+ public func flatMapError< NewFailure > (
90
+ _ transform: ( Failure ) -> Result < Success , NewFailure >
91
+ ) -> Result < Success , NewFailure > {
92
92
switch self {
93
- case let . value ( value ) :
94
- return . value ( value )
95
- case let . error ( error ) :
96
- return transform ( error )
93
+ case let . success ( success ) :
94
+ return . success ( success )
95
+ case let . failure ( failure ) :
96
+ return transform ( failure )
97
97
}
98
98
}
99
99
100
- /// Unwraps the `Result` into a throwing expression.
100
+ /// Attempts to get the `success` value as a throwing expression.
101
101
///
102
102
/// - Returns: The success value, if the instance is a success.
103
- /// - Throws: The error value, if the instance is a failure.
104
- public func unwrapped ( ) throws -> Value {
103
+ /// - Throws: The failure value, if the instance is a failure.
104
+ public func get ( ) throws -> Success {
105
105
switch self {
106
- case let . value ( value ) :
107
- return value
108
- case let . error ( error ) :
109
- throw error
106
+ case let . success ( success ) :
107
+ return success
108
+ case let . failure ( failure ) :
109
+ throw failure
110
110
}
111
111
}
112
112
}
113
113
114
- extension Result where Error == Swift . Error {
114
+ extension Result where Failure == Swift . Error {
115
115
/// Create an instance by capturing the output of a throwing closure.
116
116
///
117
117
/// - Parameter catching: A throwing closure to evaluate.
118
118
@_transparent
119
- public init ( catching body: ( ) throws -> Value ) {
119
+ public init ( catching body: ( ) throws -> Success ) {
120
120
do {
121
121
let value = try body ( )
122
- self = . value ( value)
122
+ self = . success ( value)
123
123
} catch {
124
- self = . error ( error)
124
+ self = . failure ( error)
125
125
}
126
126
}
127
127
}
128
128
129
- extension Result : Equatable where Value : Equatable , Error : Equatable { }
129
+ extension Result : Equatable where Success : Equatable , Failure : Equatable { }
130
130
131
- extension Result : Hashable where Value : Hashable , Error : Hashable { }
131
+ extension Result : Hashable where Success : Hashable , Failure : Hashable { }
0 commit comments