Try

public final class Try<A> : TryOf<A>

Describes the result of an operation that may have thrown errors or succeeded. The type parameter corresponds to the result type of the operation.

  • Creates a successful Try value.

    Declaration

    Swift

    public static func success(_ value: A) -> Try<A>

    Parameters

    value

    Value to be wrapped in a successful Try.

    Return Value

    A Try value wrapping the successful value.

  • Creates a failed Try value.

    Declaration

    Swift

    public static func failure(_ error: Error) -> Try<A>

    Parameters

    error

    An error.

    Return Value

    A Try value wrapping the error.

  • Creates a failed Try value.

    Declaration

    Swift

    public static func raise(_ error: Error) -> Try<A>

    Parameters

    error

    An error.

    Return Value

    A Try value wrapping the error.

  • Invokes a closure that may throw errors and wraps the result in a Try value.

    Declaration

    Swift

    public static func invoke(_ f: () throws -> A) -> Try<A>

    Parameters

    f

    Closure to be invoked.

    Return Value

    A Try value wrapping the result of the invocation or any error that it may have thrown.

  • Safe downcast.

    Declaration

    Swift

    public static func fix(_ fa: TryOf<A>) -> Try<A>

    Parameters

    fa

    Value in the higher-kind form.

    Return Value

    Value cast to Try.

  • Applies the provided closures based on the content of this Try value.

    Declaration

    Swift

    public func fold<B>(_ fe: (Error) -> B, _ fa: (A) throws -> B) -> B

    Parameters

    fe

    Closure to apply if the contained value in this Try is an error.

    fa

    Closure to apply if the contained value in this Try is a successful value.

    Return Value

    Result of applying the corresponding closure to this value.

  • Checks if this value is a failure.

    Declaration

    Swift

    public var isFailure: Bool { get }
  • Checks if this value is a success.

    Declaration

    Swift

    public var isSuccess: Bool { get }
  • Obtains the wrapped error in this Try.

    Declaration

    Swift

    public func failed() -> Try<Error>

    Return Value

    A successful error or a failure with TryError.unsupportedOperation if this value was not an error.

  • Obtains the value wrapped in this Try or a default value if it contains an error.

    Declaration

    Swift

    public func getOrElse(_ defaultValue: A) -> A

    Parameters

    defaultValue

    Default value for the failure case.

    Return Value

    Value wrapped in this Try, or the default value if it was an error.

  • Attempts to recover if the contained value in this Try is an error.

    Declaration

    Swift

    public func recoverWith(_ f: (Error) -> Try<A>) -> Try<A>

    Parameters

    f

    Recovery function.

    Return Value

    A Try value from the recovery, or the original value if it was not an error.

  • Recovers if the contained value in this Try is an error.

    Declaration

    Swift

    public func recover(_ f: @escaping (Error) -> A) -> Try<A>

    Parameters

    f

    Recovery function.

    Return Value

    A Try value from the recovery, or the original value if it was not an error.

  • Converts this value to a failure if the transformation provides no value.

    Declaration

    Swift

    public func mapFilter<B>(_ f: @escaping (A) -> Option<B>) -> Try<B>

    Parameters

    f

    Transformation function.

    Return Value

    A failure if the transformation of this value provides no result, or a success with the result of the transformation.

  • Converts this value to an Option.

    Declaration

    Swift

    public func toOption() -> Option<A>

    Return Value

    Option.some if this value is a success, or Option.none otherwise.

  • Converts this value to an Either.

    Declaration

    Swift

    public func toEither() -> Either<Error, A>

    Return Value

    A right value if this value is a success, or a left value if it contains an error.

  • Obtains the value of this success or the provided default one if it is a failure.

    Declaration

    Swift

    public func getOrDefault(_ defaultValue: @escaping @autoclosure () -> A) -> A

    Parameters

    defaultValue

    Function providing the default value.

    Return Value

    Wrapped value in this success, or default value otherwise.

  • Obtains the value of this success or nil if it is a failure.

    Declaration

    Swift

    public func orNil() -> A?

    Return Value

    Wrapped value in this success, or nil otherwise.

  • Obtains this value or the provided default Try if it is a failure.

    Declaration

    Swift

    public func orElse(_ f: @escaping @autoclosure () -> Try<A>) -> Try<A>

    Parameters

    f

    Function providing the default value.

    Return Value

    This value if it is a success, or the default provided one otherwise.

  • Declaration

    Swift

    public var description: String { get }
  • Declaration

    Swift

    public func combine(_ other: Try<A>) -> Try<A>
  • Declaration

    Swift

    public static func empty() -> Try<A>