Appendix B: The rest of the ecosystem
Part IV built its service from the Typelevel family of libraries, one of several that chapter 1 named. Here’s what the others are for, with a script to run wherever a few lines will show something, and then two places other than the JVM where Scala code can run. Each script goes in a directory of its own.
B.1. ZIO
ZIO is the other effect system in wide use. Save this as evening.scala and run it:
//> using scala 3.9.0
//> using jvm 25
//> using dep dev.zio::zio:2.1.27
import zio.*
object Evening extends ZIOAppDefault:
def findTable(size: Int): Task[Int] =
ZIO.sleep(100.millis).as(if size > 6 then 6 else 5)
def run =
for
_ <- Console.printLine("Fully Booked is open")
looking <- findTable(7).fork
_ <- Console.printLine("looking for a table")
table <- looking.join
_ <- Console.printLine(s"table $table is free")
yield ()
$ scala run evening.scala
Fully Booked is open
looking for a table
table 6 is free
A ZIO program is a value, as an IO is, and run chains five such programs in a for. Chapter 14’s start is called fork here, and join waits for the fiber and returns its result. ZIOAppDefault does the job of IOApp.
The nearest thing ZIO has to IO[Int] is Task[Int], a short name for one case of ZIO’s main type. That type has three parameters where IO has one: a ZIO[R, E, A] needs an R from its surroundings before it can run, can fail with an E and produces an A. The R is for things like a database or the configuration, which ZIO supplies through values it calls layers, where we passed function arguments and Play used Guice. The E does what our code did by keeping an Either inside the IO. A Task needs nothing and can fail with any Throwable.
ZIO has libraries of its own for most of what Parts III and IV used, ZIO HTTP, ZIO JSON, ZIO Streams and a test framework among them. Tapir has a ZIO server, and an interop library lets ZIO code use Cats Effect libraries such as doobie. Since ZIO and Cats Effect do the same job, I’d pick the one the team already knows and keep a service to one of them.
B.2. The Li Haoyi libraries
Li Haoyi’s libraries have no effect type at all. A call blocks until it has an answer, returns a plain value and throws if it fails. Save this one as parties.scala:
//> using scala 3.9.0
//> using jvm 25
//> using toolkit default
//> using javaOpt --sun-misc-unsafe-memory-access=allow
import upickle.default.{read, write, ReadWriter}
case class Party(name: String, size: Int) derives ReadWriter
@main def parties(): Unit =
val file = os.temp.dir() / "parties.json"
os.write(file, write(List(Party("Hughes", 4), Party("Mbeki", 8))))
println(os.read(file))
println(read[List[Party]](os.read(file)).map(_.size).sum)
$ scala run parties.scala
[{"name":"Hughes","size":4},{"name":"Mbeki","size":8}]
12
The script uses two of Li Haoyi’s libraries. OS-Lib, which the code calls os, is for files and processes, and uPickle turns case classes into JSON and back. Neither appears in a dep line, because using toolkit brings in the Scala Toolkit, a small set of libraries the Scala project recommends for everyday jobs. It holds OS-Lib and uPickle, along with sttp for HTTP requests and MUnit. Li Haoyi also wrote Requests for HTTP calls, a small web framework called Cask, and Mill, a build tool that some teams use in place of sbt.
I’d use this style for a script or a command-line tool. Li Haoyi’s book, Hands-on Scala Programming, teaches the language through these libraries.
B.3. Pekko
Pekko, the open-source fork of Akka, is what Play 3 runs on. On its own it’s a toolkit for building a program out of actors, objects that communicate only by sending messages. An actor keeps its state to itself and handles one message at a time, so that state needs no lock. On top of actors Pekko has streams, a way of spreading actors across a cluster of machines, and persistence for their state.
Pekko suits systems that keep a great deal of live state in memory across many machines. A service like Fully Booked, whose state is in a database, doesn’t need it, and I wouldn’t add actors to one. If you inherit a codebase with akka or org.apache.pekko imports outside the Play parts, start reading at receive and onMessage, the methods where an actor handles its messages.
B.4. Spark
Much of the paid work in Scala is data engineering on Apache Spark. Spark is an engine for processing large amounts of data on a cluster, and it’s written in Scala itself. A Spark job calls methods such as map and filter on a Dataset, a collection that is spread over many machines. Spark decides where each step runs.
Spark 4 is built for Scala 2.13 and has no Scala 3 build, so Spark code is normally written in the Scala 2 that appendix A describes. Spark can be driven from Python as well, and often is.
B.5. Scala.js and Scala Native
Everything in this book has run on the JVM, but the Scala compiler has two other targets, JavaScript and native executables. Save this as tables.scala:
//> using scala 3.9.0
case class Table(number: Int, seats: Int)
val tables = List(Table(1, 2), Table(3, 4), Table(5, 6), Table(6, 8))
def smallestFor(size: Int): String =
tables.filter(_.seats >= size).minByOption(_.seats) match
case Some(table) => s"table ${table.number} for a party of $size"
case None => s"nothing seats $size"
@main def seat(): Unit =
println(smallestFor(5))
println(smallestFor(9))
As usual, scala run tables.scala runs it on the JVM. With Node.js installed, adding --js compiles it to JavaScript and runs that:
$ scala run tables.scala --js
table 5 for a party of 5
nothing seats 9
With the clang compiler installed, --native makes a native executable and runs it. The command prints some twenty lines about compiling and linking first, which I’ve left out:
$ scala run tables.scala --native
table 5 for a party of 5
nothing seats 9
Scala.js is mainly for the browser. A team can write the front end in the language of the back end and share code between the two, such as validation rules or a Tapir endpoint description. Scala Native compiles to a program that needs no JVM and starts at once, which suits command-line tools. To keep the executable, run scala --power package tables.scala --native -o seat, which writes it to a file called seat. On my machine that file is 2.5 megabytes and runs in under a hundredth of a second. The --power is there because the scala command treats package as a restricted feature.
Neither target can use a Java library, since there’s no JVM underneath. Scala libraries have to be published for each target separately. Cats, Cats Effect and fs2 are published for both.