Need help with programming-scala-book-code-examples?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

537 Stars 365 Forks Other 713 Commits 4 Opened issues


The code examples used in Programming Scala, 2nd and 3rd Editions (O'Reilly)

Services available


Need anything else?

Contributors list

Programming Scala, 3rd Edition

README for the Code Examples

| Key Dates | Description | | :---------------- | :---------- | | August 11, 2014 | 2nd edition examples | | May 27, 2019 | Updated for Scala 2.12 and 2.13 | | June 18, 2019 | New support for Maven builds, courtesy of oldbig | | October 12, 2019 | Updated for Scala 2.13.1, sbt 1.3.2, and other dependencies. Also now compiles with JDK 11 | | October 13, 2019 | Renamed the repo from

| | December 31, 2019 | Renamed the
package to
and reworked most of the
scripts for better testability and other improvements | | March 1, 2020 | Completed conversion to Scala 3 | | March 20, 2020 | Started incorporating new Scala 3 syntax, idioms | | November 11, 2020 | First Scala
milestone changes | | November 25, 2020 | First Scala
milestone changes | | December 19, 2020 | First Scala
milestone changes | | February 21, 2021 | Scala
updates | | April 3, 2021 | Scala
updates | | April 24, 2021 | Scala
updates | | May 15, 2021 | Scala
final updates. Almost done! | | May 22, 2021 | Final updates for Programming Scala, Third Edition! | | July 24, 2021 | Scala 3.0.1. Notes on using IntelliJ. |

Join the chat at

This repo contains all the code examples in Programming Scala, Third Edition. (The second edition is available here.) There are also many code files in this distribution that aren't included in the book.


branch and the
tag releases are for the third edition. The code examples for the second edition are still available. Download the release tagged 2.1.0 or check out the
branch. While the second edition was published for 2.11. The latest
release and
are updated for 2.12 and 2.13. (No more
releases are planned.)

TIP: Several sections offer troubleshooting tips if you encounter problems.

How the Code Is Used in the Book

In the book's text, when an example corresponds to a file in this distribution, the listing begins with a path in a comment with the following format:

// src/main/scala/progscala3/.../FooBar.scala

Following the usual conventions, tests are in


Use these comments to find the corresponding source file. This archive also contains MUnit and ScalaCheck unit tests to validate some of the code.

Naming Conventions

The examples include "scripts" that are intended for interactive use in the

command-line "REPL" (read, eval, print loop), for example using
sbt console
is the de facto build tool for Scala that I use). Other files are compiled.

To keep these different kinds of files straight and to support building with

, the following conventions are used for the files:
  • src/main/scala/.../*.scala
    - All Scala 3 source files built with
  • src/test/.../*.scala
    - All Scala 3 test source files built and executed with
  • src/script/.../*.scala
    - "Script" files that won't compile with
    , but can be interpreted with the
    REPL or used as a worksheet (see below).
  • src/*/scala-2/.../*.scala
    - Some Scala 2 source files that won't compile with Scala 3. They are not built with

Other Notes about the Code

You won't find many comments in the code, except of the form

// <1>
, which get converted into markers corresponding to notes in the book. All the descriptions of the code are in the book, so they aren't repeated as code comments.

Some files have sections marked like this:

// tag::section1[]
// end::section1[]

These are used to mark sections that are selectively included in the book. Sometimes the whole file is included in sections, while other times the file has extra bonus content that doesn't appear in the book.

Required and Optional Tools

To build and run the examples, all you need is a recent version of the the JDK and

. When you run

, it will bootstrap itself with the correct version of its jar file, Scala, and project dependencies, which are specified in the
file in the root directory and other build files in the

Follow these

installation instructions.

If you want to install Scala separately and Scala's Scaladocs, go to the Getting Started guide for details. However, this isn't required.

If you want to play with the Spark example,

, you'll need to download a Spark distribution from Assuming that
refers to the root directory of your Spark installation, run the following command in the root directory of this project:
$ $SPARK_HOME/bin/spark-shell

Then copy and paste the content of

at the prompt. After it runs, there will be a new directory,
with the partition files of the output.

Tip: For more on Spark, see my free tutorial on GitHub, spark-scala-tutorial.

Editors, IntelliJ, Visual Studio Code, and Other IDEs

NOTE: Support for Scala 3 may be limited for a while in the following tools.

Most editors and IDEs now have some sort of Scala support:

For other IDEs and text editors, try Scala Metals first (I've used it with Sublime Text, for example) or the older ENSIME project. You may also need additional, third-party tools for syntax highlighting, etc.

After installing the required plugins, load this project in your IDE, which should detect and use the

project automatically. For eclipse, run the
sbt eclipse
task to generate project files, then import them.

Troubleshooting with IntelliJ

One reader reported a problem when trying to run examples in IntelliJ:

scalac: Flag -encoding set repeatedly
. I could confirm this problem and I fixed it as follows:
  1. Open the preferences ("cmd-," on MacOS)
  2. Search for "scala"
  3. Select "Build, Execution, Deployment > Compiler > Scala Compiler"
  4. Select the "sbt" configuration in the list of Scala build configurations.
  5. Select "Additional compiler options:".
  6. Remove
    -encoding utf-8
    from the text, since it is already in the

After that, you should be able to select a type with a

and run it.

The same reader also reported errors where multiple occurrences of the same name in a

annotation collided. I believe this happens if you use
in a terminal to compile and then allow IntelliJ to do its own build. There are probably two copies of the class files on the resulting runtime classpath. For example, I saw this error when attempting to run
sbt console
in IntelliJ's
sbt shell
, but not when I used
in a terminal window.

So, what worked for me was to only use the terminal to run

sbt clean
, then let IntelliJ build the software itself, but when I need to use
sbt console
, I use a terminal window.

Using Scala Worksheets

If you like working with Scala worksheets in your IDE or editor, you may be able to load any of the REPL "script" files under

as a worksheet. If your environment is more restrictive, for example about the file extension used, then run the included
to copy all the REPL "script" examples to worksheet files. This command copies the tree
and changes the
extension for all the files to
, the VSCode convention. These behaviors are configurable. Use the
option to see the details. If you are using Windows and you don't have
available, e.g., through the Linux subsystem, then modify individual files as you see fit.

See this Dotty documentation page for more information about worksheets.

Building the Code Examples

After installing

, open a command/terminal window and run the
sbt test

You'll see lots of output as it downloads all the dependencies, compiles the code and runs the tests. You should see

messages at the end.

is discussed in more detail in the book and the
, but a few useful commands are worth mentioning here.

If you start

without any arguments, it puts you into an interactive mode where you can type commands. Use control-D to exit this mode. Once at the
prompt (
), try the following commands, where each
starts a comment; don't type those!
help       # help on tasks and settings
clean      # delete all build outputs
compile    # compile the source, but not test code
test       # compile source and test code, if necessary and run the tests.
~test      # continuously compile and test when source changes are saved.
console    # run the Scala REPL; dependencies and code are on the CLASSPATH
tasks      # show the most common tasks (commands).
tasks -V   # REALLY show ALL tasks


prefix causes the task to be run continuously each time source code changes are saved. This promotes continuous TDD (test-driven development) and is one of my favorite features!

Outside of

, you could, in principle, run the REPL and load the script files manually at the prompt:
$ scala
scala> :load src/script/scala/.../Foo.scala

However, it's easier to run most of the scripts using

sbt console
, because
will configure the
with the third-party libraries and compiled code examples that a script file might use.

Also, new for the Scala 3 REPL, for those

files that define one (and only one) entry point, meaning a
method (Scala 2 compatible) or annotated with
(new Scala 3 technique), you can compile and run them in one step:
$ scala src/main/scala/progscala3/introscala/UpperMain2.scala Hello World!

Build Troubleshooting

Here are tips for issues users have reported.


I didn't check that the tests pass or the scripts work if your language environment isn't English. So, no surprise I guess, a German user reported that three tests failed when running

sbt test
while the
was set to
[info] Passed: Total 7, Failed 0, Errors 0, Passed 7
[error] Failed: Total 233, Failed 7, Errors 0, Passed 226
[error] Failed tests:
[error]         progscala3.fp.combinators.PayrollSuite
[error]         progscala3.fp.curry.TupledFuncSuite
[error]         progscala3.dsls.payroll.PayrollSuite
[error] (Test / test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed 31.07.2021, 16:56:39


export LANG="en_US.UTF-8"; sbt test
works. A PR for this is welcome ;) As an interim step, you could wrap this logic (or the more concise single command
LANG="en_US.UTF-8" sbt test
) in a script or just ignore the three failing tests.


I welcome feedback on the Book and these examples. Please post comments, corrections, etc. to one of the following places:

There is also my dedicated site for the book where occasional updates, clarifications, corrections, and lame excuses will be posted:

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.