Learn Scala

Expressive JVM language fusing functional and object-oriented programming

24 lessons · 5 hours · 4 levels · covers Scala 3.7

Scala is a statically typed language for the JVM (and JavaScript and native) that combines object-oriented and functional programming with a powerful type system and concise syntax. It powers data and streaming platforms such as Apache Spark, Kafka tooling and Akka/Pekko systems, as well as backend services at many companies. Learning Scala 3 teaches you immutable data modelling, pattern matching, type classes and safe concurrency that carry over to any language.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics8 lessons · 1 hour in total
2. PracticalUse it productively at work, with the usual idioms15 lessons · 3 hours in total
3. AdvancedThe less common features, performance and trade-offs20 lessons · 4 hours in total
4. ExpertInternals and the edge cases experts know24 lessons · 5 hours in total

The Scala tree

ScalaRunning Scala and @mainRunning Scala and@mainValues, variables and typesValues, variables andtypesExpressions and control structuresExpressions andcontrol structuresMethods and function valuesMethods and functionvaluesClasses, objects and companionsClasses, objects andcompanionsCase classesCase classesPattern matchingPattern matchingImmutable collectionsImmutablecollectionsTraits and mixinsTraits and mixinsEnums and algebraic data typesEnums and algebraicdata typesOption, Either and TryOption, Either andTryFor comprehensionsFor comprehensionsHigher-order functions and curryingHigher-orderfunctions and curry…Collections in depth: views and mutabilityCollections in depth:views and mutabilitysbt projects and testing with MUnitsbt projects andtesting with MUnit
Essentials and Practical shown (15 of 24 lessons); the full list is below. Click a lesson to read it.

All 24 lessons, in order

Basics

  1. Running Scala and @main · Essentials · 8 min

    Run Scala 3 programs with the scala command (Scala CLI), write a @main method and use the REPL.

  2. Values, variables and types · Essentials · 10 min

    val versus var, type inference, the basic types and string interpolation.

  3. Expressions and control structures · Essentials · 10 min

    if, while, for loops and blocks, where almost everything is an expression that returns a value.

  4. Pattern matching · Essentials · 12 min

    Use match expressions with literal, type, constructor, tuple and guard patterns.

Functions

  1. Methods and function values · Essentials · 12 min

    Define methods with def, use default and named arguments, and pass functions as values.

  2. Higher-order functions and currying · Practical · 12 min

    Write functions that take or return functions, curry parameter lists and compose behaviour.

Classes and traits

  1. Classes, objects and companions · Essentials · 12 min

    Define classes with constructor parameters, singleton objects and companion objects.

  2. Case classes · Essentials · 10 min

    Immutable data classes with structural equality, copy, toString and pattern matching for free.

  3. Traits and mixins · Practical · 12 min

    Define interfaces with abstract and concrete members, and mix several traits into a class.

  4. Enums and algebraic data types · Practical · 14 min

    Model closed sets of alternatives with enum and sealed traits, and match on them exhaustively.

Collections

  1. Immutable collections · Essentials · 14 min

    List, Vector, Set and Map, and the core transformations map, filter and foldLeft.

  2. Collections in depth: views and mutability · Practical · 14 min

    Pick the right collection for performance, use views and LazyList for laziness, and use mutable collections deliberately.

Effects and errors

  1. Option, Either and Try · Practical · 14 min

    Represent missing values and failures as data with Option, Either and Try instead of null and exceptions.

  2. For comprehensions · Practical · 12 min

    Chain flatMap, map and withFilter readably over Options, Eithers, collections and Futures.

  3. Futures and asynchronous code · Advanced · 15 min

    Run work asynchronously with Future and ExecutionContext, compose results and handle failures.

JVM and tooling

  1. sbt projects and testing with MUnit · Practical · 15 min

    Structure a project with build.sbt, add dependencies with %%, and write tests with MUnit.

  2. Java interoperability · Advanced · 12 min

    Use Java libraries from Scala, convert collections, handle nulls and expose Scala code to Java.

  3. Compilation to the JVM and TASTy · Expert · 16 min

    How Scala constructs map to bytecode, what TASTy files are, and the performance consequences.

Type system

  1. Generics, variance and bounds · Advanced · 16 min

    Type parameters, covariance (+A), contravariance (-A) and upper/lower bounds.

  2. Givens, using clauses and type classes · Advanced · 16 min

    Contextual abstractions: provide instances with given, require them with using, and build type classes.

  3. Extension methods · Advanced · 10 min

    Add methods to existing types with extension, and combine them with type classes for fluent syntax.

  4. Opaque, union and intersection types · Expert · 15 min

    Zero-cost type wrappers with opaque types, and precise types with A | B and A & B.

  5. Inline and macros · Expert · 20 min

    Compile-time programming with inline, compiletime ops, quotes and splices.

  6. Type-level programming · Expert · 18 min

    Match types, type lambdas, singleton types and tuple operations computed by the compiler.