ZIO cheat sheet

November 25, 2018

https://github.com/scalaz/scalaz-zio

sbt configuration

/***
libraryDependencies += "org.scalaz" %% "scalaz-zio" % "0.3.2"
*/

RTS

val rts: scalaz.zio.RTS =
  new scalaz.zio.RTS {}

A program with side-effects

val program: scalaz.zio.IO[Nothing,java.util.Date] =
  scalaz.zio.IO.sync(new java.util.Date())

Running the program

val er: scalaz.zio.ExitResult[Nothing,java.util.Date] =
  rts.unsafeRunSync(program)

er match {
  case scalaz.zio.ExitResult.Completed(v) =>
    println(s"success: ${v}")
  case scalaz.zio.ExitResult.Failed(e, ts) =>
    println(s"failure: ${e}, ${ts}")
  case scalaz.zio.ExitResult.Terminated(e) =>
    println(s"terminated: ${e}")
}

Demo

This file is literate Scala, and can be run using Codedown:

$ curl -s https://earldouglas.com/posts/reference/zio.md |
  codedown scala > script.scala
$ sbt -Dsbt.main.class=sbt.ScriptMain script.scala
success: Sun Nov 25 02:47:37 MST 2018