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.

  • Provides an Iso to go from/to this type to its Kind version.

    Declaration

    Swift

    static var fixIso: Iso<Try<A>, TryOf<A>> { get }
  • Provides a Fold based on the Foldable instance of this type.

    Declaration

    Swift

    static var fold: Fold<Try<A>, A> { get }
  • Provides a Traversal based on the Traverse instance of this type.

    Declaration

    Swift

    static var traversal: Traversal<Try<A>, A> { get }
  • Undocumented

    Declaration

    Swift

    public typealias EachFoci = A
  • Declaration

    Swift

    public static var each: PTraversal<Try<A>, Try<A>, A, A> { get }
  • Provides a polymorphic prism focused on the success side of a Try.

    Declaration

    Swift

    static func pSuccessPrism<B>() -> PPrism<Try<A>, Try<B>, A, B>

    Return Value

    A polymorphic prism focused on the success side of a Try.

  • Provides a prism focused on the success side of a Try.

    Declaration

    Swift

    static var successPrism: Prism<Try<A>, A> { get }
  • Provides a prism focused on the failure side of a Try.

    Declaration

    Swift

    static var failurePrism: Prism<Try<A>, Error> { get }
  • Provides a polymorphic Iso between Try and Either.

    Declaration

    Swift

    static func toPEither<B>() -> PIso<Try<A>, Try<B>, Either<Error, A>, Either<Error, B>>

    Return Value

    A polymorphic Iso between Try and Either.

  • Provides an Iso between Try and Either.

    Declaration

    Swift

    static var toEither: Iso<Try<A>, Either<Error, A>> { get }
  • Provides a polymorphic Iso between Try and Validated.

    Declaration

    Swift

    static func toPValidated<B>() -> PIso<Try<A>, Try<B>, Validated<Error, A>, Validated<Error, B>>

    Return Value

    A polymorphic Iso between Try and Validated.

  • Provides an Iso between Try and Validated.

    Declaration

    Swift

    static var toValidated: Iso<Try<A>, Validated<Error, A>> { get }
  • Converts this Try into a Result value.

    Declaration

    Swift

    func toResult() -> Result<A, Error>

    Return Value

    A Result.success if this is a Try.success, or a Result.failure if this is a Try.failure.

  • 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>