10 days with the ZIO 1/10
Tags: zio, functional, scala
Published: 2019-12-13 13:09:59

---
title: 10 days with the ZIO 1/10
published: true
description: mini-series of tutorials to spend some time with the ZIO, at the forefront of hip functional scala.
tags: zio, fp, scala, functional
---

I’m always in pursue in ways to scale down software development. Why? Because to scale up you need to scale down!

When the size of the software system you are building goes up it becomes critical to have fast feedback loop, to be able to prototype new features and to debug quickly. You therefore need scalable tools in both ways.

# Say Hello World to the ZIO

The ZIO is a functional library to program in Scala that scales very well in both directions. In this mini series we are going to spend some time with the ZIO building 10 small projects. We will start with a Hello World program, but first a digression on Mill.

![Mill logo](https://thepracticaldev.s3.amazonaws.com/i/1vbluwhn1wn8ch5wqxbd.png)

## Mill vs SBT

I like SBT, I think it’s much better than Maven and Gradle. But I think it’s common opinion that it’s a fairly complex machine. It’s the poster child of an era in which everyone was coming up with his own DSL and exploiting all the most powerful scala features. Since you have to basically learn another language to build a scala project, it means you have to spend a lot of time coming up with the right solution for your particular problem with specialized SBT knowledge. Lastly it’s quite slow.
Mill it’s an underrated build tool which resembles regular scala programming. It’s fairly easy to write a `build.sc` (equivalent to `build.sbt`) off the top of your head.
To install it just `brew install mill` or the equivalent for your operating system. 

### A blank slate

Let’s create a new folder `hello-world-zio` and inside it create a `build.sc` file with the following content.

```scala
// hello-world-zio/build.sc
import mill._, scalalib._

object zioHelloWorld extends ScalaModule {
  def scalaVersion = "2.13.0"
  val zioVersion = "1.0.3"
  def ivyDeps = Agg(
    ivy"dev.zio::zio:${zioVersion}"
  )
}
```

This means that we are creating a scala module called zioHelloWorld with the ZIO as dependency. The expected folder structure is very easy to remember

```
hello-world-zio/zioHelloWorld/src
```

### Hello World from the ZIO

We can create the expected file at the above folder `Hello.scala`. We can put inside the simplest ZIO app I can think of:

```scala
import zio._, zio.console._

object Hello extends App {
  def run(args: List[String]) = putStrLn("Hello World").exitCode
}
```
Note that the `App` is really `zio.App` not the `App` provided with scala. The `run` has a slightly different signature. `putStrLn` comes from the `Console` module and by default it just prints to standard output. The final `exitCode` is a convenience method that maps a success of the program to a `0` to the operating system or a `1` in case of an error. Many number correspond to a different error, here is a link to a [comprehensive list of errors](https://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html))

### Run, run. Click on run!

To run our program you can use the command

```
mill zioHelloWorld
```

You should see `Hello World` printed at the end of what seems to be some logging by `mill` itself.

Here is a working version on [Scastie](https://scastie.scala-lang.org/4UFFrto4S9msPQWwil89eg) if you didn't follow along.


## Conclusions

We have demonstrated how to the ZIO (with Mill) can easily scale down to a Hello World. We are going to use this minimalist template to build on top of it. Next up will be a word counter application.

## Day 2

[Go to day 2](https://dev.to/gurghet/10-days-with-the-zio-2-10-4jpg)