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)
-
Provide the result value for success, and
nilfor failure.Declaration
Swift
public var ok: T? { get } -
Provide the error value for errors, and
nilfor success.Declaration
Swift
public var err: E? { get }
-
Map the
Resultto a newResultby 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
Resultto a newResultby applying the given function to the underlying value for success, or propagating the error for failure. Returnsnilif the function returns nil.Declaration
Swift
public func map<NewT>(_ f: (T) throws -> NewT?) rethrows -> Result<NewT, E>? -
Map the
Resultto a newResultby 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
Resultto a newResultby applying the given function to the underlying value for success, or propagating the error for failure. Takes a function which returns aResult, 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
selfandotherare success, returnself. Otherwise return the first error in the pair.Declaration
Swift
public func and(_ other: Result<T, E>) -> Result<T, E> -
If either
selforotherare 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 singleResultwhose 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
Resultcontains an error, throw that error. NOTE: The error type must be something throwable (i.e. conforms toSwift.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, theResultcontains that value as success. If it throws, theResultcontains the thrown error as an error.Declaration
Swift
public init(_ f: @autoclosure () throws -> T)
Result Enumeration Reference