Result

enum Result

A value that represents either a success or a failure, including an associated value in each case.

Cases

case failure Required

A failure, storing a Failure value.

Declaration

case failure(: Failure)
case success Required

A success, storing a Success value.

Declaration

case success(: Success)

Instance Methods

func flatMap(_ transform: (Success) -> Result<NewSuccess, Failure>) -> Result<NewSuccess, Failure> Required

Returns a new result, mapping any success value using the given transformation and unwrapping the produced result.

  • Parameter transform: A closure that takes the success value of the instance.

Declaration

public func flatMap<NewSuccess>(_ transform: (Success) -> Result<NewSuccess, Failure>) -> Result<NewSuccess, Failure>
func flatMapError(_ transform: (Failure) -> Result<Success, NewFailure>) -> Result<Success, NewFailure> Required

Returns a new result, mapping any failure value using the given transformation and unwrapping the produced result.

  • Parameter transform: A closure that takes the failure value of the instance.

Declaration

public func flatMapError<NewFailure>(_ transform: (Failure) -> Result<Success, NewFailure>) -> Result<Success, NewFailure> where NewFailure: Error
func get() throws -> Success Required

Returns the success value as a throwing expression.

Use this method to retrieve the value of this result if it represents a success, or to catch the value if it represents a failure.

let integerResult: Result<Int, Error> = .success(5)
do {
    let value = try integerResult.get()
    print("The value is \(value).")
} catch error {
    print("Error retrieving the value: \(error)")
}
// Prints "The value is 5."

Declaration

public func get() throws -> Success
func map(_ transform: (Success) -> NewSuccess) -> Result<NewSuccess, Failure> Required

Returns a new result, mapping any success value using the given transformation.

Use this method when you need to transform the value of a Result instance when it represents a success. The following example transforms the integer success value of a result into a string:

func getNextInteger() -> Result<Int, Error> { /* ... */ }

let integerResult = getNextInteger()
// integerResult == .success(5)
let stringResult = integerResult.map({ String($0) })
// stringResult == .success("5")
  • Parameter transform: A closure that takes the success value of this instance.

Declaration

public func map<NewSuccess>(_ transform: (Success) -> NewSuccess) -> Result<NewSuccess, Failure>
func mapError(_ transform: (Failure) -> NewFailure) -> Result<Success, NewFailure> Required

Returns a new result, mapping any failure value using the given transformation.

Use this method when you need to transform the value of a Result instance when it represents a failure. The following example transforms the error value of a result by wrapping it in a custom Error type:

struct DatedError: Error {
    var error: Error
    var date: Date

    init(_ error: Error) {
        self.error = error
        self.date = Date()
    }
}

let result: Result<Int, Error> = // ...
// result == .failure(<error value>)
let resultWithDatedError = result.mapError({ e in DatedError(e) })
// result == .failure(DatedError(error: <error value>, date: <date>))
  • Parameter transform: A closure that takes the failure value of the instance.

Declaration

public func mapError<NewFailure>(_ transform: (Failure) -> NewFailure) -> Result<Success, NewFailure> where NewFailure: Error