Skip to main content
Version: 2.x

Console

The Console service contains simple I/O operations for reading/writing strings from/to the standard input, output, and error console.

FunctionInput TypeOutput Type
printline: => StringZIO[Any, IOException, Unit]
printErrorline: => StringZIO[Any, IOException, Unit]
printLineline: => StringZIO[Any, IOException, Unit]
printLineErrorline: => StringZIO[Any, IOException, Unit]
readLineZIO[Any, IOException, String]

All functions of the Console service are effectful, this means they are just descriptions of reading/writing from/to the console.

As ZIO data type supports monadic operations, we can compose these functions with for-comprehension which helps us to write our program pretty much like an imperative program:

import java.io.IOException

import zio._
import zio.Console._

object MyHelloApp extends ZIOAppDefault {
val program: ZIO[Any, IOException, Unit] = for {
_ <- printLine("Hello, what is you name?")
name <- readLine
_ <- printLine(s"Hello $name, welcome to ZIO!")
} yield ()

def run = program
}

Note again, every line of our program are descriptions, not statements. As we can see the type of our program is ZIO[Any, IOException, Unit], it means to run program we do not need any environment, it may fail due to failure of readLine and it will produce Unit value.