Skip to main content
Version: 2.x

Getting Started with ZIO

Installationโ€‹

Include ZIO in your project by adding the following to your build.sbt file:

libraryDependencies += "dev.zio" %% "zio" % "2.1.27"

Mainโ€‹

Your application can extend ZIOAppDefault, which provides a complete runtime system and allows you to write your whole program using ZIO:

import zio._
import zio.Console._

object MyApp extends ZIOAppDefault {

def run = myAppLogic

val myAppLogic =
for {
_ <- printLine("Hello! What is your name?")
name <- readLine
_ <- printLine(s"Hello, ${name}, welcome to ZIO!")
} yield ()
}

The run method should return a ZIO value which has all its errors handled,
which, in ZIO parlance, is an unexceptional ZIO value.

One way to do this is to invoke fold over a ZIO value, to get an unexceptional ZIO value. That requires two handler functions: eh: E => B (the error handler) and ah: A => B (the success handler).

If myAppLogic fails, eh will be used to get from e: E to b: B; if it succeeds, ah will be used to get from a: A to b: B.

myAppLogic, as folded above, produces an unexceptional ZIO value, with B being Int.
If myAppLogic fails, there will be a 1; if it succeeds, there will be a 0.


If you are integrating ZIO into an existing application, using dependency injection, or do not control your main function, then you can create a runtime system in order to execute your ZIO programs:

import zio._

object IntegrationExample {
val runtime = Runtime.default

Unsafe.unsafe { implicit unsafe =>
runtime.unsafe.run(ZIO.attempt(println("Hello World!"))).getOrThrowFiberFailure()
}
}

Ideally, your application should have a single runtime, because each runtime has its own resources (including thread pool and unhandled error reporter).

Consoleโ€‹

ZIO provides a Console service for interacting with the console.

If you need to print text to the console, you can use print and printLine:

import zio._

// Print without trailing line break
Console.print("Hello World")

// Print string and include trailing line break
Console.printLine("Hello World")

If you need to read input from the console, you can use readLine:

import zio._

val echo = Console.readLine.flatMap(line => Console.printLine(line))

Next Stepsโ€‹

Now that you've got ZIO installed and running, the next step is to learn about the ZIO data type.

ZIO Chat

๐ŸŒŸ Greetings from ZIO Chat! ๐ŸŒŸ Have questions about ZIO? Look no further! I'm here to be your ZIO guru. Whether you're a seasoned pro or just starting out, I've got answers tailored just for you. Fire away with your ZIO queries, and I'll hit you back with the best insights I've got. Let's dive into the world of ZIO together!