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 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 function 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>)

    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

    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.

  • Returns this IO erasing the error type information

    Declaration

    Swift

    public var anyError: IO<Error, A> { get }
  • 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.