Class/Object

cats.effect

IO

Related Docs: object IO | package effect

Permalink

sealed abstract class IO[+A] extends AnyRef

A pure abstraction representing the intention to perform a side effect, where the result of that side effect may be obtained synchronously (via return) or asynchronously (via callback).

Effects contained within this abstraction are not evaluated until the "end of the world", which is to say, when one of the "unsafe" methods are used. Effectful results are not memoized, meaning that memory overhead is minimal (and no leaks), and also that a single effect may be run multiple times in a referentially-transparent manner. For example:

val ioa = IO { println("hey!") }

val program = for {
  _ <- ioa
  _ <- ioa
} yield ()

program.unsafeRunSync()

The above will print "hey!" twice, as the effect will be re-run each time it is sequenced in the monadic chain.

IO is trampolined for all synchronous joins. This means that you can safely call flatMap in a recursive function of arbitrary depth, without fear of blowing the stack. However, IO cannot guarantee stack-safety in the presence of arbitrarily nested asynchronous suspensions. This is quite simply because it is impossible (on the JVM) to guarantee stack-safety in that case. For example:

def lie[A]: IO[A] = IO.async(cb => cb(Right(lie))).flatMap(a => a)

This should blow the stack when evaluated. Also note that there is no way to encode this using tailRecM in such a way that it does not blow the stack. Thus, the tailRecM on Monad[IO] is not guaranteed to produce an IO which is stack-safe when run, but will rather make every attempt to do so barring pathological structure.

IO makes no attempt to control finalization or guaranteed resource-safety in the presence of concurrent preemption, simply because IO does not care about concurrent preemption at all! IO actions are not interruptible and should be considered broadly-speaking atomic, at least when used purely.

Source
IO.scala
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. IO
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def attempt: IO[Either[Throwable, A]]

    Permalink

    Materializes any sequenced exceptions into value space, where they may be handled.

    Materializes any sequenced exceptions into value space, where they may be handled.

    This is analogous to the catch clause in try/catch, being the inverse of IO.raiseError. Thus:

    IO.raiseError(ex).attempt.unsafeRunAsync === Left(ex)
    See also

    IO.raiseError

  6. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  8. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  9. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  10. final def flatMap[B](f: (A) ⇒ IO[B]): IO[B]

    Permalink

    Monadic bind on IO, used for sequentially composing two IO actions, where the value produced by the first IO is passed as input to a function producing the second IO action.

    Monadic bind on IO, used for sequentially composing two IO actions, where the value produced by the first IO is passed as input to a function producing the second IO action.

    Due to this operation's signature, flatMap forces a data dependency between two IO actions, thus ensuring sequencing (e.g. one action to be executed before another one).

    Any exceptions thrown within the function will be caught and sequenced into the IO, because due to the nature of asynchronous processes, without catching and handling exceptions, failures would be completely silent and IO references would never terminate on evaluation.

  11. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  14. final def map[B](f: (A) ⇒ B): IO[B]

    Permalink

    Functor map on IO.

    Functor map on IO. Given a mapping functions, it transforms the value produced by the source, while keeping the IO context.

    Any exceptions thrown within the function will be caught and sequenced into the IO, because due to the nature of asynchronous processes, without catching and handling exceptions, failures would be completely silent and IO references would never terminate on evaluation.

  15. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  16. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  18. final def runAsync(cb: (Either[Throwable, A]) ⇒ IO[Unit]): IO[Unit]

    Permalink

    Produces an IO reference that is guaranteed to be safe to run synchronously (i.e.

    Produces an IO reference that is guaranteed to be safe to run synchronously (i.e. unsafeRunSync), being the safe analogue to unsafeRunAsync.

    This operation is isomorphic to unsafeRunAsync. What it does is to let you describe asynchronous execution with a function that stores off the results of the original IO as a side effect, thus avoiding the usage of impure callbacks or eager evaluation.

  19. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  20. final def to[F[_]](implicit F: Async[F]): F[A]

    Permalink

    Converts the source IO into any F type that implements the Async type class.

  21. def toString(): String

    Permalink
    Definition Classes
    IO → AnyRef → Any
  22. final def unsafeRunAsync(cb: (Either[Throwable, A]) ⇒ Unit): Unit

    Permalink

    Passes the result of the encapsulated effects to the given callback by running them as impure side effects.

    Passes the result of the encapsulated effects to the given callback by running them as impure side effects.

    Any exceptions raised within the effect will be passed to the callback in the Either. The callback will be invoked at most *once*. Note that it is very possible to construct an IO which never returns while still never blocking a thread, and attempting to evaluate that IO with this method will result in a situation where the callback is *never* invoked.

    As the name says, this is an UNSAFE function as it is impure and performs side effects. You should ideally only call this function once, at the very end of your program.

  23. final def unsafeRunSync(): A

    Permalink

    Produces the result by running the encapsulated effects as impure side effects.

    Produces the result by running the encapsulated effects as impure side effects.

    If any component of the computation is asynchronous, the current thread will block awaiting the results of the async computation. On JavaScript, an exception will be thrown instead to avoid generating a deadlock. By default, this blocking will be unbounded. To limit the thread block to some fixed time, use unsafeRunTimed instead.

    Any exceptions raised within the effect will be re-thrown during evaluation.

    As the name says, this is an UNSAFE function as it is impure and performs side effects, not to mention blocking, throwing exceptions, and doing other things that are at odds with reasonable software. You should ideally only call this function *once*, at the very end of your program.

  24. final def unsafeRunTimed(limit: Duration): Option[A]

    Permalink

    Similar to unsafeRunSync, except with a bounded blocking duration when awaiting asynchronous results.

    Similar to unsafeRunSync, except with a bounded blocking duration when awaiting asynchronous results.

    Please note that the limit parameter does not limit the time of the total computation, but rather acts as an upper bound on any *individual* asynchronous block. Thus, if you pass a limit of 5 seconds to an IO consisting solely of synchronous actions, the evaluation may take considerably longer than 5 seconds! Furthermore, if you pass a limit of 5 seconds to an IO consisting of several asynchronous actions joined together, evaluation may take up to n * 5 seconds, where n is the number of joined async actions.

    As soon as an async blocking limit is hit, evaluation immediately aborts and None is returned.

    Please note that this function is intended for testing; it should never appear in your mainline production code! It is absolutely not an appropriate function to use if you want to implement timeouts, or anything similar. If you need that sort of functionality, you should be using a streaming library (like fs2 or Monix).

    See also

    unsafeRunSync

  25. final def unsafeToFuture(): Future[A]

    Permalink

    Evaluates the effect and produces the result in a Future.

    Evaluates the effect and produces the result in a Future.

    This is similar to unsafeRunAsync in that it evaluates the IO as a side effect in a non-blocking fashion, but uses a Future rather than an explicit callback. This function should really only be used if interoperating with legacy code which uses Scala futures.

    See also

    IO.fromFuture

  26. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  27. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  28. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped