Preface

I wrote this book for professional Java developers who would like to start working in Scala. You should already be able to program and know your way around Java’s classes, lambdas, streams and build tools. You don’t need to have seen any Scala before, or know anything about functional programming yet.

If you’re brand new to programming, this probably isn’t the book to start with. I’ve used Cats Effect when discussing effect systems. ZIO and Spark each get a section in appendix B to give a little flavour, but it would be a big book if I gave them the same treatment.

You’ll be typing and running code from the first few pages. I explain what happened after you’ve seen it work, and the names that theory has for these things come later, once you’ve used them. Everything the book prints as output came from running its own code, on Scala 3.9 and Java 25 unless otherwise noted.

The whole book builds one project, Fully Booked, a table-booking system for a single restaurant. In chapter 1 it’s a script. By chapter 22 it’s an HTTP service with a Postgres database, tests and a Docker image.

People write Scala in two broad styles, as a more concise Java or as a functional language, and most books stick to one of them. This one starts with the first and works its way to the second.

Part I, “Scala for Java developers”, stays close to Java and covers values, case classes, collections and testing. Part II, “Getting the compiler to do the work”, brings in functions as values, types that model the problem and errors as ordinary values. Part III, “Effects”, deals with programs that do input and output and run concurrently, using the Cats Effect library. Part IV, “A real service”, gives the project a build tool, an HTTP API, a database, more tests and a container. It ends with a chapter on Play, a framework that a lot of existing Scala is written in. The two appendices cover reading Scala 2 and the parts of the ecosystem the book doesn’t use.

Scala usually gives you more than one way to do something. I’ve picked one each time. Where that choice is contentious, I’ve said why I made it and what the alternative is.

You’ll need a computer with a terminal. I’ve written the commands for macOS and Linux. Scala runs on Windows as well, though a few of the commands will need translating or a WSL terminal. Chapter 1 installs Scala, and later chapters tell you what else to install when you need it. From chapter 20 that includes Docker.

It’s best to read the chapters in order, because each one builds on the code from the one before. Every chapter ends with a few exercises. You can download the code for every chapter, with solutions to most of the exercises, from scalafoundations.com. If you get stuck or want to skip ahead, the download has the project as each chapter left it.