-
Notifications
You must be signed in to change notification settings - Fork 10.6k
SE-0235: Add Result<Success, Failure: Error> to Standard Library #20958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
33dcfc4
7fb5905
2bc4cbb
1834f0a
80ae4de
85df179
e4a700c
c739498
b167703
2bb0b5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -220,5 +220,8 @@ | |
| "Comparable.swift", | ||
| "Codable.swift", | ||
| "MigrationSupport.swift" | ||
| ], | ||
| "Result": [ | ||
| "Result.swift" | ||
| ] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2018 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| /// A value that represents either a success or failure, capturing associated | ||
| /// values in both cases. | ||
| @_frozen | ||
| public enum Result<Success, Failure: Error> { | ||
| /// A success, storing a `Success` value. | ||
| case success(Success) | ||
|
|
||
| /// A failure, storing a `Failure` value. | ||
| case failure(Failure) | ||
|
|
||
| /// Evaluates the given transform closure when this `Result` instance is | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// `.success`, passing the value as a parameter. | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// Use the `map` method with a closure that returns a non-`Result` value. | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// - Parameter transform: A closure that takes the successful value of the | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// instance. | ||
| /// - Returns: A new `Result` instance with the result of the transform, if | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// it was applied. | ||
| public func map<NewSuccess>( | ||
| _ transform: (Success) -> NewSuccess | ||
jshier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) -> Result<NewSuccess, Failure> { | ||
| switch self { | ||
| case let .success(success): | ||
| return .success(transform(success)) | ||
| case let .failure(failure): | ||
| return .failure(failure) | ||
| } | ||
| } | ||
|
|
||
| /// Evaluates the given transform closure when this `Result` instance is | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// `.failure`, passing the error as a parameter. | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// Use the `mapError` method with a closure that returns a non-`Result` | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// value. | ||
| /// | ||
| /// - Parameter transform: A closure that takes the failure value of the | ||
| /// instance. | ||
| /// - Returns: A new `Result` instance with the result of the transform, if | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// it was applied. | ||
| public func mapError<NewFailure>( | ||
| _ transform: (Failure) -> NewFailure | ||
| ) -> Result<Success, NewFailure> { | ||
| switch self { | ||
| case let .success(success): | ||
| return .success(success) | ||
| case let .failure(failure): | ||
| return .failure(transform(failure)) | ||
| } | ||
| } | ||
|
|
||
| /// Evaluates the given transform closure when this `Result` instance is | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// `.success`, passing the value as a parameter and flattening the result. | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// - Parameter transform: A closure that takes the successful value of the | ||
| /// instance. | ||
| /// - Returns: A new `Result` instance, either from the transform or from | ||
| /// the previous error value. | ||
| public func flatMap<NewSuccess>( | ||
jshier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _ transform: (Success) -> Result<NewSuccess, Failure> | ||
| ) -> Result<NewSuccess, Failure> { | ||
| switch self { | ||
| case let .success(success): | ||
| return transform(success) | ||
| case let .failure(failure): | ||
| return .failure(failure) | ||
| } | ||
| } | ||
|
|
||
| /// Evaluates the given transform closure when this `Result` instance is | ||
| /// `.failure`, passing the error as a parameter and flattening the result. | ||
| /// | ||
| /// - Parameter transform: A closure that takes the error value of the | ||
| /// instance. | ||
| /// - Returns: A new `Result` instance, either from the transform or from | ||
| /// the previous success value. | ||
| public func flatMapError<NewFailure>( | ||
| _ transform: (Failure) -> Result<Success, NewFailure> | ||
| ) -> Result<Success, NewFailure> { | ||
| switch self { | ||
| case let .success(success): | ||
| return .success(success) | ||
| case let .failure(failure): | ||
| return transform(failure) | ||
| } | ||
| } | ||
|
|
||
| /// Attempts to get the `success` value as a throwing expression. | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// - Returns: The success value, if the instance is a success. | ||
| /// - Throws: The failure value, if the instance is a failure. | ||
| public func get() throws -> Success { | ||
| switch self { | ||
| case let .success(success): | ||
| return success | ||
| case let .failure(failure): | ||
| throw failure | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension Result where Failure == Swift.Error { | ||
| /// Create an instance by capturing the output of a throwing closure. | ||
xwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// - Parameter catching: A throwing closure to evaluate. | ||
| @_transparent | ||
| public init(catching body: () throws -> Success) { | ||
| do { | ||
| let value = try body() | ||
| self = .success(value) | ||
|
||
| } catch { | ||
| self = .failure(error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension Result : Equatable where Success : Equatable, Failure : Equatable { } | ||
|
|
||
| extension Result : Hashable where Success : Hashable, Failure : Hashable { } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,171 @@ | ||||||||||||||||||||||
| // RUN: %target-run-simple-swift | ||||||||||||||||||||||
| // REQUIRES: executable_test | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import StdlibUnittest | ||||||||||||||||||||||
| import Swift | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let ResultTests = TestSuite("Result") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| fileprivate enum Err: Error, Equatable { | ||||||||||||||||||||||
| case err | ||||||||||||||||||||||
| case derr | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| fileprivate let string = "string" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| fileprivate extension Result { | ||||||||||||||||||||||
| var success: Success? { | ||||||||||||||||||||||
| switch self { | ||||||||||||||||||||||
| case let .success(success): | ||||||||||||||||||||||
| return success | ||||||||||||||||||||||
| case .failure: | ||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| var failure: Failure? { | ||||||||||||||||||||||
| switch self { | ||||||||||||||||||||||
| case .success: | ||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
| case let .failure(failure): | ||||||||||||||||||||||
| return failure | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ResultTests.test("Construction") { | ||||||||||||||||||||||
| let result1: Result<String, Err> = .success(string) | ||||||||||||||||||||||
| let result2: Result<String, Err> = .failure(.err) | ||||||||||||||||||||||
| let string1: String? = { | ||||||||||||||||||||||
| switch result1 { | ||||||||||||||||||||||
| case let .success(string): | ||||||||||||||||||||||
| return string | ||||||||||||||||||||||
| case .failure: | ||||||||||||||||||||||
| expectUnreachable() | ||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }() | ||||||||||||||||||||||
| let error: Err? = { | ||||||||||||||||||||||
| switch result2 { | ||||||||||||||||||||||
| case let .failure(failure): | ||||||||||||||||||||||
| return failure | ||||||||||||||||||||||
| case .success: | ||||||||||||||||||||||
| expectUnreachable() | ||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }() | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expectEqual(string1, string) | ||||||||||||||||||||||
jshier marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||
| expectEqual(error, .err) | ||||||||||||||||||||||
jshier marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ResultTests.test("Throwing Initialization and Unwrapping") { | ||||||||||||||||||||||
| func notThrowing() throws -> String { | ||||||||||||||||||||||
| return string | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func throwing() throws -> String { | ||||||||||||||||||||||
| throw Err.err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let result1 = Result { try throwing() } | ||||||||||||||||||||||
| let result2 = Result { try notThrowing() } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expectEqual(result1.failure as? Err, Err.err) | ||||||||||||||||||||||
| expectEqual(result2.success, string) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| do { | ||||||||||||||||||||||
| _ = try result1.get() | ||||||||||||||||||||||
| } catch let error as Err { | ||||||||||||||||||||||
| expectEqual(error, Err.err) | ||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||
| expectUnreachable() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| do { | ||||||||||||||||||||||
| let unwrapped = try result2.get() | ||||||||||||||||||||||
| expectEqual(unwrapped, string) | ||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||
| expectUnreachable() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Test unwrapping strongly typed error. | ||||||||||||||||||||||
| let result3 = Result<String, Err>.failure(Err.err) | ||||||||||||||||||||||
| do { | ||||||||||||||||||||||
| _ = try result3.get() | ||||||||||||||||||||||
| } catch let error as Err { | ||||||||||||||||||||||
| expectEqual(error, Err.err) | ||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||
| expectUnreachable() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ResultTests.test("Functional Transforms") { | ||||||||||||||||||||||
| func transformDouble(_ int: Int) -> Int { | ||||||||||||||||||||||
| return 2 * int | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func transformTriple(_ int: Int) -> Int { | ||||||||||||||||||||||
| return 3 * int | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func transformError(_ err: Err) -> Err { | ||||||||||||||||||||||
| if err == .err { | ||||||||||||||||||||||
| return .derr | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| return .err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func resultValueTransform(_ int: Int) -> Result<Int, Err> { | ||||||||||||||||||||||
| return .success(transformDouble(int)) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func resultErrorTransform(_ err: Err) -> Result<Int, Err> { | ||||||||||||||||||||||
| return .failure(transformError(err)) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let result1: Result<Int, Err> = .success(1) | ||||||||||||||||||||||
| let newResult1 = result1.map(transformDouble) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expectEqual(newResult1, .success(2)) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let result2: Result<Int, Err> = .failure(.err) | ||||||||||||||||||||||
| let newResult2 = result2.mapError(transformError) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expectEqual(newResult2, .failure(.derr)) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let result3: Result<Int, Err> = .success(1) | ||||||||||||||||||||||
| let newResult3 = result3.flatMap(resultValueTransform) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expectEqual(newResult3, .success(2)) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let result4: Result<Int, Err> = .failure(.derr) | ||||||||||||||||||||||
| let newResult4 = result4.flatMapError(resultErrorTransform) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expectEqual(newResult4, .failure(.err)) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ResultTests.test("Equatable") { | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you consider using the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, as I mostly cribbed these tests by looking at those from In general, is there any documentation around this special
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just thinking out loud really. These conformances are synthesized by the compiler, and should be working. Otherwise we're in trouble.
Unfortunately no.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's not about simplification. Just that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added a use of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: |
||||||||||||||||||||||
| let result1: Result<Int, Err> = .success(1) | ||||||||||||||||||||||
| let result2: Result<Int, Err> = .failure(.err) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expectEqual(result1, .success(1)) | ||||||||||||||||||||||
| expectNotEqual(result1, .success(2)) | ||||||||||||||||||||||
| expectNotEqual(result1, .failure(.err)) | ||||||||||||||||||||||
| expectNotEqual(result1, .failure(.derr)) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expectNotEqual(result2, .success(1)) | ||||||||||||||||||||||
| expectNotEqual(result2, .success(2)) | ||||||||||||||||||||||
| expectEqual(result2, .failure(.err)) | ||||||||||||||||||||||
| expectNotEqual(result2, .failure(.derr)) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ResultTests.test("Hashable") { | ||||||||||||||||||||||
| let result1: Result<Int, Err> = .success(1) | ||||||||||||||||||||||
| let result2: Result<Int, Err> = .success(2) | ||||||||||||||||||||||
| let result3: Result<Int, Err> = .failure(.err) | ||||||||||||||||||||||
| checkHashable([result1, result2, result3], equalityOracle: { $0 == $1 }) | ||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would also be useful to have a hashing check with potentially colliding Success/Failure hash encodings:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| runAllTests() | ||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.