Lumenary CS · Programming languages
Scala
Object-oriented and functional programming fused in one statically typed language on the JVM, from a first program to type classes, effects and the tools professionals use.
Scala is a statically typed language that first appeared in 2004, created by Martin Odersky and his group at EPFL in Lausanne. It runs on the Java Virtual Machine (and, through Scala.js and Scala Native, in browsers and as native executables), uses any Java library directly, and blends two styles that are often kept apart: every value is an object with methods, and functions are values you pass around and combine. Its type inference keeps code short, while its type system is expressive enough to catch whole classes of mistakes at compile time. Scala is used for back-end services, data engineering (Apache Spark is written in it), financial systems, compilers and research tools.
This course is for anyone who already knows what a variable, a loop and a function are: the Start level of Lumenary CS or equivalent experience in any language. It begins with installing Scala and running a first program with Scala CLI, works through values, functions, case classes, pattern matching, enums and the collections library, and goes on to traits, generics and variance, Scala 3's contextual abstractions (given, using and extension methods), functional error handling with Option, Either and Try, concurrency with Futures, an overview of the effect libraries and Spark, and the build tools of the ecosystem, ending with a complete small project.
To run the code, install Scala from scala-lang.org/download; since Scala 3.5 the scala command is Scala CLI, which compiles and runs a single file with nothing else to set up (you also need a JDK, which the installer can fetch). To try programs in the browser, use Scastie, the official online playground. Every complete program in these lessons was compiled with Scala 3.10 and run on JDK 21 by Lumenary's test suite, and the output shown is the output it printed. The lessons were drafted with AI assistance and are awaiting review by a human computer scientist.
- Chloe · The Designer
“Why is the language built this way?” - Margaret · The Profiler
“What does it print, and what does it cost?” - Gideon · The Language Lawyer
“What exactly does the language guarantee?”
Foundations · 3 lessons
Foundations
Install Scala, run programs with Scala CLI, and learn the core language: values, types and inference, expressions and control flow, and functions and methods.
- 1Getting started with ScalaWhere Scala came from, what it is trying to be, and your first programs with Scala CLI. 45 min
- 2Values, types and expressionsval and var, the basic types, type inference, and a language in which almost everything has a value. 50 min
- 3Functions and methodsMethods with def, functions as values, higher-order functions and closures, recursion, and parameters evaluated by name. 55 min
Practice · 3 lessons
Practice
Model data with case classes and enums and take it apart with pattern matching, transform it with the immutable collections, and abstract with traits, generics, variance, type classes and extension methods.
- 4Case classes, enums and pattern matchingClasses and objects, immutable data with case classes, algebraic data types with enum, and the match expression that takes them apart. 60 min
- 5CollectionsImmutable lists, vectors, sets and maps, the operations that transform them, for comprehensions, laziness, and when mutation is the right tool. 60 min
- 6Traits, generics and type classesMixins and their linearisation, type parameters and variance, and Scala 3's contextual abstractions, given, using and extension methods. 65 min
Professional · 3 lessons
Professional
Handle failure with Option, Either and Try and test it, run work concurrently with Futures, know where Cats Effect, ZIO and Spark fit, use sbt and the rest of the toolchain, and build a complete project in idiomatic style.
- 7Option, Either, Try and testingExceptions and when to use them, failure as a value with Option, Either and Try, chaining with for comprehensions, and testing with MUnit and ScalaCheck. 60 min
- 8Concurrency and the ecosystemFutures and execution contexts, the eager-start pitfall, virtual threads, where Cats Effect, ZIO, Pekko and Spark fit, and the build tools and linters of professional Scala. 65 min
- 9Project: a calculator languageA complete interpreter in idiomatic Scala 3, with a lexer, a recursive-descent parser, an evaluator, a simplifier and a property test, and where to go next. 75 min
Further reading and resources
- The Scala Programming LanguageThe official website: downloads, news, the community and the Scala Center.
- Scala DocumentationThe official documentation hub: the Scala 3 Book, guides, overviews and the style guide.
- Scala 3 BookThe official, free introduction to the language, a good companion to this course.
- Scala 3 ReferencePrecise descriptions of each Scala 3 feature, from enums to givens and opaque types.
- Scala Language Specification (Scala 2.13)The formal specification of Scala 2.13; Scala 3's changes are documented in the Scala 3 Reference.
- Scala Standard Library APIEvery class and method of the standard library.
- Scala CLIThe command-line tool behind the
scalacommand: compile, run, test and package. - ScastieThe official online playground: write and run Scala in the browser.