Result

public enum Result<T, E>

A value which is either a successful result, or an error.

  • A successful result.

    Declaration

    Swift

    case Ok(T)
  • An error.

    Declaration

    Swift

    case Err(E)
  • ok

    Provide the result value for success, and nil for failure.

    Declaration

    Swift

    public var ok: T? { get }
  • err

    Provide the error value for errors, and nil for success.

    Declaration

    Swift

    public var err: E? { get }
  • Map the Result to a new Result by applying the given function to the underlying value for success, or propagating the error for failure.

    Declaration

    Swift

    public func map<NewT>(_ f: (T) throws -> NewT) rethrows -> Result<NewT, E>
  • Map the Result to a new Result by applying the given function to the underlying value for success, or propagating the error for failure. Returns nil if the function returns nil.

    Declaration

    Swift

    public func map<NewT>(_ f: (T) throws -> NewT?) rethrows -> Result<NewT, E>?
  • Map the Result to a new Result by applying the given function to the error, or propagating the value for success.

    Declaration

    Swift

    public func mapErr<NewE>(_ f: (E) throws -> NewE) rethrows -> Result<T, NewE>
  • Map the Result to a new Result by applying the given function to the underlying value for success, or propagating the error for failure. Takes a function which returns a Result, and returns the error if that function returns an error.

    Declaration

    Swift

    public func flatMap<NewT>(_ f: (T) throws -> Result<NewT, E>) rethrows -> Result<NewT, E>
  • The same as flatMap.

    Declaration

    Swift

    public func then<NewT>(_ f: (T) throws -> Result<NewT, E>) rethrows -> Result<NewT, E>
  • If both self and other are success, return self. Otherwise return the first error in the pair.

    Declaration

    Swift

    public func and(_ other: Result<T, E>) -> Result<T, E>
  • If either self or other are success, return the first value in the pair. Otherwise return the first error in the pair.

    Declaration

    Swift

    public func or(_ other: Result<T, E>) -> Result<T, E>
  • Transform two Results into a single Result whose value is a tuple containing the two original values.

    Declaration

    Swift

    public func combine<U>(_ other: Result<U, E>) -> Result<(T, U), E>
  • Return the underlying value. If the Result contains an error, throw that error. NOTE: The error type must be something throwable (i.e. conforms to Swift.Error) but this can’t be enforced in the type system currently, so it’s up to you to only call this when the type is appropriate.

    Declaration

    Swift

    public func orThrow() throws -> T
  • Convert a throwing expression into a Result. If the expression returns a value, the Result contains that value as success. If it throws, the Result contains the thrown error as an error.

    Declaration

    Swift

    public init(_ f: @autoclosure () throws -> T)