Generic Enumeration

Result

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

Declaration

enum Result<Success, Failure> where Failure : Error

Topics

Representing a Result

case success(Success)

A success, storing a Success value.

case failure(Failure)

A failure, storing a Failure value.

Writing Failable Asynchronous APIs

Vend results as part of an API when you can’t return errors synchronously.

Converting a Throwing Expression to a Result

Preserving the Results of a Throwing Expression

Call the initializer that wraps a throwing expression when you need to serialize or memoize the result.

init(catching: () -> Success)

Creates a new result by evaluating a throwing closure, capturing the returned value as a success, or any thrown error as a failure.

Converting a Result to a Throwing Expression

func get() -> Success

Returns the success value as a throwing expression.

Transforming a Result

func map<NewSuccess>((Success) -> NewSuccess) -> Result<NewSuccess, Failure>

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

func mapError<NewFailure>((Failure) -> NewFailure) -> Result<Success, NewFailure>

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

func flatMap<NewSuccess>((Success) -> Result<NewSuccess, Failure>) -> Result<NewSuccess, Failure>

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

func flatMapError<NewFailure>((Failure) -> Result<Success, NewFailure>) -> Result<Success, NewFailure>

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

Relationships

Conforms To

  • Equatable
  • Hashable

See Also

Errors

protocol Error

A type representing an error value that can be thrown.