ZIO Cheat Sheet

March 25, 2023 ยท View on GitHub

  • This is based on ZIO 2.0.X (in particular 2.0.10).
  • For simplicity, ZIO environment has been omitted but all the functions also work with the form ZIO[R, E, A].
  • Function arguments are usually by name, but that has (mostly) been ignored for simplicity. Also, functions are often "more generic" in their parameter types than shown below.
  • For many functions there are several (unmentioned) related functions that are conceptually similar but differ in some detail. They are usually easy to learn due to consistent naming.
  • Important ZIO types other than the functional effect type ZIO[R, E, A] have been left out. For example: ZStream[R, E, A], ZLayer[RIn, E, ROut], Fiber[E, A] and Ref[A].
  • In the remainder of this cheat sheet, E1 >: E, but E2 can be any error type. Also A1 >: A.

Aliases

AliasFull Form
UIO[A]ZIO[Any, Nothing, A]
IO[E, A]ZIO[Any, E, A]
Task[A]ZIO[Any, Throwable, A]
RIO[R, A]ZIO[R, Throwable, A]
URIO[R, A]ZIO[R, Nothing, A]

Creating effects

NameGivenTo
ZIO.succeed=> AIO[Nothing, A]
ZIO.fail=> EIO[E, Nothing]
ZIO.interruptIO[Nothing, Nothing]
ZIO.dieThrowableIO[Nothing, Nothing]
ZIO.attempt=> AIO[Throwable, A]
ZIO.async((IO[E, A] => Unit) => Unit)
FiberId
IO[E, A]
ZIO.fromEitherEither[E, A]IO[E, A]
ZIO.leftAIO[Nothing, Either[A, Nothing]]
ZIO.rightBIO[Nothing, Either[Nothing, B]]
ZIO.fromFiberFiber[E, A]IO[E, A]
ZIO.fromFutureExecutionContext => Future[A]IO[Throwable, A]
ZIO.fromOptionOption[A]IO[Option[Nothing], A]
ZIO.noneIO[Nothing, Option[Nothing]]
ZIO.someAIO[Nothing, Option[A]]
ZIO.fromTryTry[A]IO[Throwable, A]
ZIO.acquireReleaseWithIO[E, A] (acquire)
A => IO[Nothing, Any] (release)
A => IO[E, B] (use)
IO[E, B]
ZIO.whenBoolean
IO[E, A]
IO[E, Option[A]]
ZIO.whenZIOIO[E, Boolean]
IO[E, A]
IO[E, Option[A]]
ZIO.whenCaseA
PartialFunction[A, IO[E, B]]
IO[E, Option[B]]
ZIO.whenCaseZIOIO[E, A]
PartialFunction[A, IO[E, B]]
IO[E, Option[B]]
ZIO.filterIterable[A]
A => IO[E, Boolean]
IO[E, List[A]]
ZIO.condBoolean
A
E
IO[E, A]

Transforming effects

NameFromGivenTo
mapIO[E, A]A => BIO[E, B]
mapAttemptIO[Throwable, A]A => BIO[Throwable, B]
asIO[E, A]BIO[E, B]
asSomeIO[E, A]IO[E, Option[A]]
asSomeErrorIO[E, A]IO[Option[E], A]
orElseFailIO[E, A]E2IO[E2, A]
unitIO[E, A]IO[E, Unit]
flatMapIO[E, A]A => IO[E1, B]IO[E1, B]
flattenIO[E, IO[E1, A]]IO[E1, A]
mapBothIO[E, A]E => E2
A => B
IO[E2, B]
mapErrorIO[E, A]E => E2IO[E2, A]
mapErrorCauseIO[E, A]Cause[E] => Cause[E2]IO[E2, A]
flatMapErrorIO[E, A]E => IO[Nothing, E2]IO[E2, A]
collectIO[E, A]E1
PartialFunction[A, B]
IO[E1, B]
sandboxIO[E, A]IO[Cause[E], A]
flipIO[E, A]IO[A, E]
tapIO[E, A]A => IO[E1, _]IO[E1, A]
tapBothIO[E, A]E => IO[E1, _]
A => IO[E1, _]
IO[E1, A]
tapErrorIO[E, A]E => IO[E1, _]IO[E1, A]
absolveIO[E, Either[E, A]]IO[E, A]
someIO[E, Option[A]]IO[Option[E], A]
headIO[E, List[A]]IO[Option[E], A]
toFutureIO[Throwable, A]IO[Nothing, Future[A]]
filterOrDieIO[E, A]A => Boolean
Throwable
IO[E, A]
filterOrDieMessageIO[E, A]A => Boolean
String
IO[E, A]
filterOrElseIO[E, A]A => Boolean
A => IO[E, A]
IO[E, A]
filterOrFailIO[E, A]A => Boolean
E
IO[E, A]
whenIO[E, A]BooleanIO[E, Option[A]]
unlessIO[E, A]BooleanIO[E, Option[A]]

Declaring and providing the environment

NameFromGivenTo
ZIO.servicegiven Tag[A]ZIO[A, Nothing, A]
provideEnvironmentZIO[R, E, A]ZEnvironment[R]IO[E, A]
provideLayerZIO[R, E, A]ZLayer[R0, E, R]ZIO[R0, E, A]

Configuration

NameFromGivenTo
ZIO.configConfig[A]IO[Config.Error, A]

Recover from errors

NameFromGivenTo
eitherIO[E, A]IO[Nothing, Either[E, A]]
optionIO[E, A]IO[Nothing, Option[A]]
ignoreIO[E, A]IO[Nothing, Unit]
exitIO[E, A]IO[Nothing, Exit[E, A]]
<> (orElse)IO[E, A]IO[E2, A1]IO[E2, A1]
orElseEitherIO[E, A]IO[E2, B]IO[E2, Either[A, B]]
foldIO[E, A]E => B
A => B
IO[Nothing, B]
foldZIOIO[E, A]E => IO[E2, B]
A => IO[E2, B]
IO[E2, B]
foldCauseZIOIO[E, A]Cause[E] => IO[E2, B]
A => IO[E2, B]
IO[E2, B]
catchAllIO[E, A]E => IO[E2, A1]IO[E2, A1]
catchAllCauseIO[E, A]Cause[E] => IO[E2, A1]IO[E2, A1]
catchSomeIO[E, A]PartialFunction[E, IO[E1, A1]]IO[E1, A1]
retryIO[E, A]Schedule[E, S]IO[E, A]
eventuallyIO[E, A]IO[Nothing, A]

Terminate fiber with errors

NameFromGivenTo
orDieIO[Throwable, A]IO[Nothing, A]
orDieWithIO[E, A]E => ThrowableIO[Nothing, A]
refineOrDieIO[Throwable, A]PartialFunction[Throwable, E2]IO[E2, A]
refineOrDieWithIO[E, A]PartialFunction[E, E2]
E => Throwable
IO[E2, A]

Combining effects + parallelism

NameFromGivenTo
ZIO.foldLeftIterable[A]
S
(S, A) => IO[E, S]
IO[E, S]
ZIO.foreachIterable[A]
A => IO[E, B]
IO[E, List[B]]
ZIO.foreachParIterable[A]
A => IO[E, B]
IO[E, List[B]]
ZIO.collectAllIterable[IO[E, A]]IO[E, List[A]]
ZIO.collectAllParIterable[IO[E, A]]IO[E, List[A]]
ZIO.forkAllIterable[IO[E, A]]IO[Nothing, Fiber[E, List[A]]]
forkIO[E, A]IO[Nothing, Runtime[E, A]]
<*> (zip)IO[E, A]IO[E1, B]IO[E1, (A, B)]
*> (zipRight)IO[E, A]IO[E1, B]IO[E1, B]
<* (zipLeft)IO[E, A]IO[E1, B]IO[E1, A]
<&> (zipPar)IO[E, A]IO[E1, B]IO[E1, (A, B)]
&> (zipParRight)IO[E, A]IO[E1, B]IO[E1, B]
<& (zipParLeft)IO[E, A]IO[E1, B]IO[E1, A]
raceIO[E, A]IO[E1, A1]IO[E1, A1]
raceAllIO[E, A]Iterable[IO[E1, A1]]IO[E1, A1]
raceEitherIO[E, A]IO[E1, B]IO[E1, Either[A, B]]

Finalizers

NameFromGivenTo
ensuringIO[E, A]UIO[_]IO[E, A]
onErrorIO[E, A]Cause[E] => UIO[_]IO[E, A]
onInterruptIO[E, A]UIO[_]IO[E, A]
onTerminationIO[E, A]Cause[Nothing] => UIO[_]IO[E, A]

Timing and repetition

NameFromGivenTo
ZIO.neverIO[Nothing, Nothing]
ZIO.sleepDurationIO[Nothing, Unit]
delayIO[E, A]DurationIO[E, A]
timeoutIO[E, A]DurationIO[E, Option[A]]
timedIO[E, A]IO[E, (Duration, A)]
foreverIO[E, A]IO[E, Nothing]
repeatIO[E, A]Schedule[A, B]IO[E, B]
repeatUntilIO[E, A]A => BooleanIO[E, A]
repeatWhileIO[E, A]A => BooleanIO[E, A]

Logging

NameFromGivenTo
ZIO.logStringIO[Nothing, Unit]
ZIO.logFatalStringIO[Nothing, Unit]
ZIO.logErrorStringIO[Nothing, Unit]
ZIO.logWarningStringIO[Nothing, Unit]
ZIO.logInfoStringIO[Nothing, Unit]
ZIO.logDebugStringIO[Nothing, Unit]
ZIO.logTraceStringIO[Nothing, Unit]