Skip to content

Commit 2bc4cbb

Browse files
committed
Update for revised proposal.
1 parent 7fb5905 commit 2bc4cbb

File tree

2 files changed

+96
-96
lines changed

2 files changed

+96
-96
lines changed

stdlib/public/core/Result.swift

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,35 @@
1313
/// A value that represents either a success or failure, capturing associated
1414
/// values in both cases.
1515
@_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)
1919

20-
/// A failure, storing an `Error`.
21-
case error(Error)
20+
/// A failure, storing a `Failure` value.
21+
case failure(Failure)
2222

2323
/// 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.
2525
///
2626
/// Use the `map` method with a closure that returns a non-`Result` value.
2727
///
2828
/// - Parameter transform: A closure that takes the successful value of the
2929
/// instance.
3030
/// - Returns: A new `Result` instance with the result of the transform, if
3131
/// 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> {
3535
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)
4040
}
4141
}
4242

4343
/// 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.
4545
///
4646
/// Use the `mapError` method with a closure that returns a non-`Result`
4747
/// value.
@@ -50,82 +50,82 @@ public enum Result<Value, Error: Swift.Error> {
5050
/// instance.
5151
/// - Returns: A new `Result` instance with the result of the transform, if
5252
/// 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> {
5656
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))
6161
}
6262
}
6363

6464
/// 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.
6666
///
6767
/// - Parameter transform: A closure that takes the successful value of the
6868
/// instance.
6969
/// - Returns: A new `Result` instance, either from the transform or from
7070
/// 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> {
7474
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)
7979
}
8080
}
8181

8282
/// 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.
8484
///
8585
/// - Parameter transform: A closure that takes the error value of the
8686
/// instance.
8787
/// - Returns: A new `Result` instance, either from the transform or from
8888
/// 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> {
9292
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)
9797
}
9898
}
9999

100-
/// Unwraps the `Result` into a throwing expression.
100+
/// Attempts to get the `success` value as a throwing expression.
101101
///
102102
/// - 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 {
105105
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
110110
}
111111
}
112112
}
113113

114-
extension Result where Error == Swift.Error {
114+
extension Result where Failure == Swift.Error {
115115
/// Create an instance by capturing the output of a throwing closure.
116116
///
117117
/// - Parameter catching: A throwing closure to evaluate.
118118
@_transparent
119-
public init(catching body: () throws -> Value) {
119+
public init(catching body: () throws -> Success) {
120120
do {
121121
let value = try body()
122-
self = .value(value)
122+
self = .success(value)
123123
} catch {
124-
self = .error(error)
124+
self = .failure(error)
125125
}
126126
}
127127
}
128128

129-
extension Result : Equatable where Value : Equatable, Error : Equatable { }
129+
extension Result : Equatable where Success : Equatable, Failure : Equatable { }
130130

131-
extension Result : Hashable where Value : Hashable, Error : Hashable { }
131+
extension Result : Hashable where Success : Hashable, Failure : Hashable { }

test/stdlib/Result.swift

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,42 @@ fileprivate enum Err: Error, Equatable {
1414
fileprivate let string = "string"
1515

1616
fileprivate extension Result {
17-
var value: Value? {
17+
var success: Success? {
1818
switch self {
19-
case let .value(value):
20-
return value
21-
case .error:
19+
case let .success(success):
20+
return success
21+
case .failure:
2222
return nil
2323
}
2424
}
2525

26-
var error: Error? {
26+
var failure: Failure? {
2727
switch self {
28-
case .value:
28+
case .success:
2929
return nil
30-
case let .error(error):
31-
return error
30+
case let .failure(failure):
31+
return failure
3232
}
3333
}
3434
}
3535

3636
ResultTests.test("Construction") {
37-
let result1: Result<String, Err> = .value(string)
38-
let result2: Result<String, Err> = .error(.err)
37+
let result1: Result<String, Err> = .success(string)
38+
let result2: Result<String, Err> = .failure(.err)
3939
let string1: String? = {
4040
switch result1 {
41-
case let .value(string):
41+
case let .success(string):
4242
return string
43-
case .error:
43+
case .failure:
4444
expectUnreachable()
4545
return nil
4646
}
4747
}()
4848
let error: Err? = {
4949
switch result2 {
50-
case let .error(error):
51-
return error
52-
case .value:
50+
case let .failure(failure):
51+
return failure
52+
case .success:
5353
expectUnreachable()
5454
return nil
5555
}
@@ -71,28 +71,28 @@ ResultTests.test("Throwing Initialization and Unwrapping") {
7171
let result1 = Result { try throwing() }
7272
let result2 = Result { try notThrowing() }
7373

74-
expectEqual(result1.error as? Err, Err.err)
75-
expectEqual(result2.value, string)
74+
expectEqual(result1.failure as? Err, Err.err)
75+
expectEqual(result2.success, string)
7676

7777
do {
78-
_ = try result1.unwrapped()
78+
_ = try result1.get()
7979
} catch let error as Err {
8080
expectEqual(error, Err.err)
8181
} catch {
8282
expectUnreachable()
8383
}
8484

8585
do {
86-
let unwrapped = try result2.unwrapped()
86+
let unwrapped = try result2.get()
8787
expectEqual(unwrapped, string)
8888
} catch {
8989
expectUnreachable()
9090
}
9191

9292
// Test unwrapping strongly typed error.
93-
let result3 = Result<String, Err>.error(Err.err)
93+
let result3 = Result<String, Err>.failure(Err.err)
9494
do {
95-
_ = try result3.unwrapped()
95+
_ = try result3.get()
9696
} catch let error as Err {
9797
expectEqual(error, Err.err)
9898
} catch {
@@ -118,53 +118,53 @@ ResultTests.test("Functional Transforms") {
118118
}
119119

120120
func resultValueTransform(_ int: Int) -> Result<Int, Err> {
121-
return .value(transformDouble(int))
121+
return .success(transformDouble(int))
122122
}
123123

124124
func resultErrorTransform(_ err: Err) -> Result<Int, Err> {
125-
return .error(transformError(err))
125+
return .failure(transformError(err))
126126
}
127127

128-
let result1: Result<Int, Err> = .value(1)
128+
let result1: Result<Int, Err> = .success(1)
129129
let newResult1 = result1.map(transformDouble)
130130

131-
expectEqual(newResult1, .value(2))
131+
expectEqual(newResult1, .success(2))
132132

133-
let result2: Result<Int, Err> = .error(.err)
133+
let result2: Result<Int, Err> = .failure(.err)
134134
let newResult2 = result2.mapError(transformError)
135135

136-
expectEqual(newResult2, .error(.derr))
136+
expectEqual(newResult2, .failure(.derr))
137137

138-
let result3: Result<Int, Err> = .value(1)
138+
let result3: Result<Int, Err> = .success(1)
139139
let newResult3 = result3.flatMap(resultValueTransform)
140140

141-
expectEqual(newResult3, .value(2))
141+
expectEqual(newResult3, .success(2))
142142

143-
let result4: Result<Int, Err> = .error(.derr)
143+
let result4: Result<Int, Err> = .failure(.derr)
144144
let newResult4 = result4.flatMapError(resultErrorTransform)
145145

146-
expectEqual(newResult4, .error(.err))
146+
expectEqual(newResult4, .failure(.err))
147147
}
148148

149149
ResultTests.test("Equatable") {
150-
let result1: Result<Int, Err> = .value(1)
151-
let result2: Result<Int, Err> = .error(.err)
152-
153-
expectEqual(result1, .value(1))
154-
expectNotEqual(result1, .value(2))
155-
expectNotEqual(result1, .error(.err))
156-
expectNotEqual(result1, .error(.derr))
157-
158-
expectNotEqual(result2, .value(1))
159-
expectNotEqual(result2, .value(2))
160-
expectEqual(result2, .error(.err))
161-
expectNotEqual(result2, .error(.derr))
150+
let result1: Result<Int, Err> = .success(1)
151+
let result2: Result<Int, Err> = .failure(.err)
152+
153+
expectEqual(result1, .success(1))
154+
expectNotEqual(result1, .success(2))
155+
expectNotEqual(result1, .failure(.err))
156+
expectNotEqual(result1, .failure(.derr))
157+
158+
expectNotEqual(result2, .success(1))
159+
expectNotEqual(result2, .success(2))
160+
expectEqual(result2, .failure(.err))
161+
expectNotEqual(result2, .failure(.derr))
162162
}
163163

164164
ResultTests.test("Hashable") {
165-
let result1: Result<Int, Err> = .value(1)
166-
let result2: Result<Int, Err> = .value(2)
167-
let result3: Result<Int, Err> = .error(.err)
165+
let result1: Result<Int, Err> = .success(1)
166+
let result2: Result<Int, Err> = .success(2)
167+
let result3: Result<Int, Err> = .failure(.err)
168168
checkHashable([result1, result2, result3], equalityOracle: { $0 == $1 })
169169
}
170170

0 commit comments

Comments
 (0)