|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +/// A value that represents either a success or a failure, including an |
| 14 | +/// associated value in each case. |
| 15 | +@_frozen |
| 16 | +public enum Result<Success, Failure: Error> { |
| 17 | + /// A success, storing a `Success` value. |
| 18 | + case success(Success) |
| 19 | + |
| 20 | + /// A failure, storing a `Failure` value. |
| 21 | + case failure(Failure) |
| 22 | + |
| 23 | + /// Returns a new result, mapping any success value using the given |
| 24 | + /// transformation. |
| 25 | + /// |
| 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 |
| 38 | + /// instance. |
| 39 | + /// - Returns: A `Result` instance with the result of evaluating `transform` |
| 40 | + /// as the new success value if this instance represents a success. |
| 41 | + public func map<NewSuccess>( |
| 42 | + _ transform: (Success) -> NewSuccess |
| 43 | + ) -> Result<NewSuccess, Failure> { |
| 44 | + switch self { |
| 45 | + case let .success(success): |
| 46 | + return .success(transform(success)) |
| 47 | + case let .failure(failure): |
| 48 | + return .failure(failure) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 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>)) |
| 73 | + /// |
| 74 | + /// - Parameter transform: A closure that takes the failure value of the |
| 75 | + /// instance. |
| 76 | + /// - Returns: A `Result` instance with the result of evaluating `transform` |
| 77 | + /// as the new failure value if this instance represents a failure. |
| 78 | + public func mapError<NewFailure>( |
| 79 | + _ transform: (Failure) -> NewFailure |
| 80 | + ) -> Result<Success, NewFailure> { |
| 81 | + switch self { |
| 82 | + case let .success(success): |
| 83 | + return .success(success) |
| 84 | + case let .failure(failure): |
| 85 | + return .failure(transform(failure)) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Returns a new result, mapping any success value using the given |
| 90 | + /// transformation and unwrapping the produced result. |
| 91 | + /// |
| 92 | + /// - Parameter transform: A closure that takes the success value of the |
| 93 | + /// instance. |
| 94 | + /// - Returns: A `Result` instance with the result of evaluating `transform` |
| 95 | + /// as the new failure value if this instance represents a failure. |
| 96 | + public func flatMap<NewSuccess>( |
| 97 | + _ transform: (Success) -> Result<NewSuccess, Failure> |
| 98 | + ) -> Result<NewSuccess, Failure> { |
| 99 | + switch self { |
| 100 | + case let .success(success): |
| 101 | + return transform(success) |
| 102 | + case let .failure(failure): |
| 103 | + return .failure(failure) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /// Returns a new result, mapping any failure value using the given |
| 108 | + /// transformation and unwrapping the produced result. |
| 109 | + /// |
| 110 | + /// - Parameter transform: A closure that takes the failure value of the |
| 111 | + /// instance. |
| 112 | + /// - Returns: A `Result` instance, either from the closure or the previous |
| 113 | + /// `.success`. |
| 114 | + public func flatMapError<NewFailure>( |
| 115 | + _ transform: (Failure) -> Result<Success, NewFailure> |
| 116 | + ) -> Result<Success, NewFailure> { |
| 117 | + switch self { |
| 118 | + case let .success(success): |
| 119 | + return .success(success) |
| 120 | + case let .failure(failure): |
| 121 | + return transform(failure) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 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." |
| 138 | + /// |
| 139 | + /// - Returns: The success value, if the instance represent a success. |
| 140 | + /// - Throws: The failure value, if the instance represents a failure. |
| 141 | + public func get() throws -> Success { |
| 142 | + switch self { |
| 143 | + case let .success(success): |
| 144 | + return success |
| 145 | + case let .failure(failure): |
| 146 | + throw failure |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +extension Result where Failure == Swift.Error { |
| 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. |
| 154 | + /// |
| 155 | + /// - Parameter body: A throwing closure to evaluate. |
| 156 | + @_transparent |
| 157 | + public init(catching body: () throws -> Success) { |
| 158 | + do { |
| 159 | + self = .success(try body()) |
| 160 | + } catch { |
| 161 | + self = .failure(error) |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +extension Result: Equatable where Success: Equatable, Failure: Equatable { } |
| 167 | + |
| 168 | +extension Result: Hashable where Success: Hashable, Failure: Hashable { } |
0 commit comments