DEV Community

Andrea
Andrea

Posted on • Edited on

10 days with the ZIO 1/10

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

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.

// 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}"
  )
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

import zio._, zio.console._

object Hello extends App {
  def run(args: List[String]) = putStrLn("Hello World").exitCode
}
Enter fullscreen mode Exit fullscreen mode

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)

Run, run. Click on run!

To run our program you can use the command

mill zioHelloWorld
Enter fullscreen mode Exit fullscreen mode

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 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

Top comments (0)