Skip to main content
Version: 1.0.18

Console

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

FunctionInput TypeOutput Type
putStrline: => StringURIO[Console, Unit]
putStrErrline: => StringURIO[Console, Unit]
putStrLnline: => StringURIO[Console, Unit]
putStrLnErrline: => StringURIO[Console, Unit]
getStrLnZIO[Console, IOException, String]

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

As ZIO data type support 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.ZIO
import zio.console._

object MyHelloApp extends zio.App {
val program: ZIO[Console, IOException, Unit] = for {
_ <- putStrLn("Hello, what is you name?")
name <- getStrLn
_ <- putStrLn(s"Hello $name, welcome to ZIO!")
} yield ()

override def run(args: List[String]) = program.exitCode
}

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