IO

public class IO<E, A> : IOOf<E, A> where E : Error

An IO is a data type that encapsulates and suspends side effects producing values of type A or errors of type E.

  • Safe downcast.

    Declaration

    Swift

    public static func fix(_ fa: IOOf<E, A>) -> IO<E, A>

    Parameters

    fa

    Value in higher-kind form.

    Return Value

    Value casted to IO.

  • Creates an EnvIO with no dependencies from this IO.

    Declaration

    Swift

    public func env<D>() -> EnvIO<D, E, A>
  • Creates an IO from a side-effectful function.

    Declaration

    Swift

    public static func invoke(_ f: @escaping () throws -> A) -> IO<E, A>

    Parameters

    f

    Side-effectful function. Errors thrown from this function must be of type E; otherwise, a fatal error will happen.

    Return Value

    An IO suspending the execution of the side effect.

  • Creates an IO from a side-effectful function.

    Declaration

    Swift

    public static func invokeEither(_ f: @escaping () throws -> Either<E, A>) -> IO<E, A>

    Parameters

    f

    Side-effectful function returning an Either. Errors thrown from this function must be of type E; otherwise, a fatal error will happen.

    Return Value

    An IO suspending the execution of the side effect.

  • Creates an IO from a side-effectful function.

    Declaration

    Swift

    public static func invokeResult(_ f: @escaping () throws -> Result<A, E>) -> IO<E, A>

    Parameters

    f

    Side-effectful function returning a Result. Errors thrown from this function must be of type E; otherwise, a fatal error will happen.

    Return Value

    An IO suspending the execution of the side effect.

  • Creates an IO from a side-effectful function.

    Declaration

    Swift

    public static func invokeValidated(_ f: @escaping () throws -> Validated<E, A>) -> IO<E, A>

    Parameters

    f

    Side-effectful function returning an Validated. Errors thrown from this function must be of type E; otherwise, a fatal error will happen.

    Return Value

    An IO suspending the execution of the side effect.

  • Creates an IO from 2 side-effectful functions, tupling their results. Errors thrown from the functions must be of type E; otherwise, a fatal error will happen.

    Declaration

    Swift

    public static func merge<Z, B>(
        _ fa: @escaping () throws -> Z,
        _ fb: @escaping () throws -> B) -> IO<E, (Z, B)> where A == (Z, B)

    Parameters

    fa

    1st side-effectful function.

    fb

    2nd side-effectful function.

    Return Value

    An IO suspending the execution of the side effects.

  • Creates an IO from 3 side-effectful functions, tupling their results. Errors thrown from the functions must be of type E; otherwise, a fatal error will happen.

    Declaration

    Swift

    public static func merge<Z, B, C>(
        _ fa: @escaping () throws -> Z,
        _ fb: @escaping () throws -> B,
        _ fc: @escaping () throws -> C) -> IO<E, (Z, B, C)> where A == (Z, B, C)

    Parameters

    fa

    1st side-effectful function.

    fb

    2nd side-effectful function.

    fc

    3rd side-effectful function.

    Return Value

    An IO suspending the execution of the side effects.

  • Creates an IO from 4 side-effectful functions, tupling their results. Errors thrown from the functions must be of type E; otherwise, a fatal error will happen.

    Declaration

    Swift

    public static func merge<Z, B, C, D>(
        _ fa: @escaping () throws -> Z,
        _ fb: @escaping () throws -> B,
        _ fc: @escaping () throws -> C,
        _ fd: @escaping () throws -> D) -> IO<E, (Z, B, C, D)> where A == (Z, B, C, D)

    Parameters

    fa

    1st side-effectful function.

    fb

    2nd side-effectful function.

    fc

    3rd side-effectful function.

    fd

    4th side-effectful function.

    Return Value

    An IO suspending the execution of the side effects.

  • Creates an IO from 5 side-effectful functions, tupling their results. Errors thrown from the functions must be of type E; otherwise, a fatal error will happen.

    Declaration

    Swift

    public static func merge<Z, B, C, D, F>(
        _ fa: @escaping () throws -> Z,
        _ fb: @escaping () throws -> B,
        _ fc: @escaping () throws -> C,
        _ fd: @escaping () throws -> D,
        _ ff: @escaping () throws -> F) -> IO<E, (Z, B, C, D, F)> where A == (Z, B, C, D, F)

    Parameters

    fa

    1st side-effectful function.

    fb

    2nd side-effectful function.

    fc

    3rd side-effectful function.

    fd

    4th side-effectful function.

    ff

    5th side-effectful function.

    Return Value

    An IO suspending the execution of the side effects.

  • Creates an IO from 6 side-effectful functions, tupling their results. Errors thrown from the functions must be of type E; otherwise, a fatal error will happen.

    Declaration

    Swift

    public static func merge<Z, B, C, D, F, G>(
        _ fa: @escaping () throws -> Z,
        _ fb: @escaping () throws -> B,
        _ fc: @escaping () throws -> C,
        _ fd: @escaping () throws -> D,
        _ ff: @escaping () throws -> F,
        _ fg: @escaping () throws -> G) -> IO<E, (Z, B, C, D, F, G)> where A == (Z, B, C, D, F, G)

    Parameters

    fa

    1st side-effectful function.

    fb

    2nd side-effectful function.

    fc

    3rd side-effectful function.

    fd

    4th side-effectful function.

    ff

    5th side-effectful function.

    fg

    6th side-effectful function.

    Return Value

    An IO suspending the execution of the side effects.

  • Creates an IO from 7 side-effectful functions, tupling their results. Errors thrown from the functions must be of type E; otherwise, a fatal error will happen.

    Declaration

    Swift

    public static func merge<Z, B, C, D, F, G, H>(
        _ fa: @escaping () throws -> Z,
        _ fb: @escaping () throws -> B,
        _ fc: @escaping () throws -> C,
        _ fd: @escaping () throws -> D,
        _ ff: @escaping () throws -> F,
        _ fg: @escaping () throws -> G,
        _ fh: @escaping () throws -> H ) -> IO<E, (Z, B, C, D, F, G, H)> where A == (Z, B, C, D, F, G, H)

    Parameters

    fa

    1st side-effectful function.

    fb

    2nd side-effectful function.

    fc

    3rd side-effectful function.

    fd

    4th side-effectful function.

    ff

    5th side-effectful function.

    fg

    6th side-effectful function.

    fh

    7th side-effectful function.

    Return Value

    An IO suspending the execution of the side effects.

  • Creates an IO from 8 side-effectful functions, tupling their results. Errors thrown from the functions must be of type E; otherwise, a fatal error will happen.

    Declaration

    Swift

    public static func merge<Z, B, C, D, F, G, H, I>(
        _ fa: @escaping () throws -> Z,
        _ fb: @escaping () throws -> B,
        _ fc: @escaping () throws -> C,
        _ fd: @escaping () throws -> D,
        _ ff: @escaping () throws -> F,
        _ fg: @escaping () throws -> G,
        _ fh: @escaping () throws -> H,
        _ fi: @escaping () throws -> I) -> IO<E, (Z, B, C, D, F, G, H, I)> where A == (Z, B, C, D, F, G, H, I)

    Parameters

    fa

    1st side-effectful function.

    fb

    2nd side-effectful function.

    fc

    3rd side-effectful function.

    fd

    4th side-effectful function.

    ff

    5th side-effectful function.

    fg

    6th side-effectful function.

    fh

    7th side-effectful function.

    fi

    8th side-effectful function.

    Return Value

    An IO suspending the execution of the side effects.

  • Creates an IO from 9 side-effectful functions, tupling their results. Errors thrown from the functions must be of type E; otherwise, a fatal error will happen.

    Declaration

    Swift

    public static func merge<Z, B, C, D, F, G, H, I, J>(
        _ fa: @escaping () throws -> Z,
        _ fb: @escaping () throws -> B,
        _ fc: @escaping () throws -> C,
        _ fd: @escaping () throws -> D,
        _ ff: @escaping () throws -> F,
        _ fg: @escaping () throws -> G,
        _ fh: @escaping () throws -> H,
        _ fi: @escaping () throws -> I,
        _ fj: @escaping () throws -> J ) -> IO<E, (Z, B, C, D, F, G, H, I, J)> where A == (Z, B, C, D, F, G, H, I, J)

    Parameters

    fa

    1st side-effectful function.

    fb

    2nd side-effectful function.

    fc

    3rd side-effectful function.

    fd

    4th side-effectful function.

    ff

    5th side-effectful function.

    fg

    6th side-effectful function.

    fh

    7th side-effectful function.

    fi

    8th side-effectful function.

    fj

    9th side-effectful function.

    Return Value

    An IO suspending the execution of the side effects.

  • Performs the side effects that are suspended in this IO in a synchronous manner.

    Throws

    Error of type E that may happen during the evaluation of the side-effects. Errors of other types thrown from the evaluation of this IO will cause a fatal error.

    Declaration

    Swift

    public func unsafeRunSync(on queue: DispatchQueue = .main) throws -> A

    Parameters

    queue

    Dispatch queue used to execute the side effects. Defaults to the main queue.

    Return Value

    Value produced after running the suspended side effects.

  • Performs the side effects that are suspended in this IO in a synchronous manner.

    Declaration

    Swift

    public func unsafeRunSyncEither(on queue: DispatchQueue = .main) -> Either<E, A>

    Parameters

    queue

    Dispatch queue used to execute the side effects. Defaults to the main queue.

    Return Value

    An Either wrapping errors in the left side and values on the right side. Errors of other types thrown from the evaluation of this IO will cause a fatal error.

  • Flattens the internal structure of this IO by performing the side effects and wrapping them again in an IO structure containing the result or the error produced.

    Declaration

    Swift

    public func attempt(on queue: DispatchQueue = .main) -> IO<E, A>

    Parameters

    queue

    Dispatch queue used to execute the side effects. Defaults to the main queue.

    Return Value

    An IO that either contains the value produced or an error. Errors of other types thrown from the evaluation of this IO will cause a fatal error.

  • Performs the side effects that are suspended in this IO in an asynchronous manner.

    Declaration

    Swift

    public func unsafeRunAsync(on queue: DispatchQueue = .main, _ callback: @escaping Callback<E, A> = { _ in })

    Parameters

    queue

    Dispatch queue used to execute the side effects. Defaults to the main queue.

    callback

    A callback function to receive the results of the evaluation. Errors of other types thrown from the evaluation of this IO will cause a fatal error.

  • Transforms the error type of this IO.

    Declaration

    Swift

    @available(*, deprecated, renamed: "mapError")
    public func mapLeft<EE>(_ f: @escaping (E) -> EE) -> IO<EE, A>

    Parameters

    f

    Function transforming the error.

    Return Value

    An IO value with the new error type.

  • Transforms the error type of this IO.

    Declaration

    Swift

    public func mapError<EE>(_ f: @escaping (E) -> EE) -> IO<EE, A>

    Parameters

    f

    Function transforming the error.

    Return Value

    An IO value with the new error type.

  • Returns this IO erasing the error type information

    Declaration

    Swift

    public var anyError: IO<Error, A> { get }
  • Folds over the result of this computation by accepting an effect to execute in case of error, and another one in the case of success.

    Declaration

    Swift

    public func foldM<B>(
        _ f: @escaping (E) -> IO<E, B>,
        _ g: @escaping (A) -> IO<E, B>) -> IO<E, B>

    Parameters

    f

    Function to run in case of error.

    g

    Function to run in case of success.

    Return Value

    A computation from the result of applying the provided functions to the result of this computation.

  • Retries this computation if it fails based on the provided retrial policy.

    This computation will be at least executed once, and if it fails, it will be retried according to the policy.

    Declaration

    Swift

    public func retry<S, O>(_ policy: Schedule<Any, E, S, O>) -> IO<E, A>

    Parameters

    policy

    Retrial policy.

    Return Value

    A computation that is retried based on the provided policy when it fails.

  • Retries this computation if it fails based on the provided retrial policy, providing a default computation to handle failures after retrial.

    This computation will be at least executed once, and if it fails, it will be retried according to the policy.

    Declaration

    Swift

    public func retry<S, O, B>(
        _ policy: Schedule<Any, E, S, O>,
        orElse: @escaping (E, O) -> IO<E, B>) -> IO<E, Either<B, A>>

    Parameters

    policy

    Retrial policy.

    orElse

    Function to handle errors after retrying.

    Return Value

    A computation that is retried based on the provided policy when it fails.

  • Repeats this computation until the provided repeating policy completes, or until it fails.

    This computation will be at least executed once, and if it succeeds, it will be repeated additional times according to the policy.

    Declaration

    Swift

    public func `repeat`<S, O>(
        _ policy: Schedule<Any, A, S, O>,
        onUpdateError: @escaping () -> E = { fatalError("Impossible to update error on repeat.") }) -> IO<E, O>

    Parameters

    policy

    Repeating policy.

    onUpdateError

    A function providing an error in case the policy fails to update properly.

    Return Value

    A computation that is repeated based on the provided policy when it succeeds.

  • Repeats this computation until the provided repeating policy completes, or until it fails, with a function to handle potential failures.

    Declaration

    Swift

    public func `repeat`<S, O, B>(
        _ policy: Schedule<Any, A, S, O>,
        onUpdateError: @escaping () -> E = { fatalError("Impossible to update error on repeat.") },
        orElse: @escaping (E, O?) -> IO<E, B>) -> IO<E, Either<B, O>>

    Parameters

    policy

    Repeating policy.

    onUpdateError

    A function providing an error in case the policy fails to update properly.

    orElse

    A function to return a computation in case of error.

    Return Value

    A computation that is repeated based on the provided policy when it succeeds.

  • Sleeps for the specified amount of time.

    Declaration

    Swift

    static func sleep(_ interval: DispatchTimeInterval) -> IO<E, Void>

    Parameters

    interval

    Interval of time to sleep.

    Return Value

    An IO that sleeps for the specified amount of time.

  • Sleeps for the specified amount of time.

    Declaration

    Swift

    static func sleep(_ interval: TimeInterval) -> IO<E, Void>

    Parameters

    interval

    Interval of time to sleep.

    Return Value

    An IO that sleeps for the specified amount of time.

  • Creates an IO from a side-effectful function.

    Declaration

    Swift

    static func invokeTry(_ f: @escaping () throws -> Try<A>) -> IO<Error, A>

    Parameters

    f

    Side-effectful function returning a Try. Errors thrown from this function must be of type E; otherwise, a fatal error will happen.

    Return Value

    An IO suspending the execution of the side effect.