Generic Enumeration

Result

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

Declaration

@frozen 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.

Publishing a Result

struct Result.Publisher

A Combine publisher that publishes a result and then finishes normally, or fails without publishing any elements.

Instance Properties

var hashValue: Int

The hash value.

Instance Methods

func hash(into: inout Hasher)

Hashes the essential components of this value by feeding them into the given hasher.

Operator Functions

static func == (Result<Success, Failure>, Result<Success, Failure>) -> Bool

Returns a Boolean value indicating whether two values are equal.

Relationships

Conforms To

  • Equatable
  • Hashable

See Also

Errors

protocol Error

A type representing an error value that can be thrown.