Work in Progress

Scala 3 has not been released, yet. We are still in the process of writing the documentation for Scala 3. You can help us to improve the documentation.

Are you searching for the Scala 2 documentation?

Scala 3 — Book

Types and the Type System


Scala is a unique language in that it’s statically typed, but often feels flexible and dynamic. For instance, thanks to type inference you can write code like this without explicitly specifying the variable types:

val a = 1
val b = 2.0
val c = "Hi!"

That makes the code feel dynamically typed. And thanks to new features, like union types in Scala 3, you can also write code like the following that expresses very concisely which values are expected as arguments and which types are returned:

def isTruthy(a: Boolean | Int | String): Boolean = ???
def dogCatOrWhatever(): Dog | Plant | Car | Sun = ???

As the example suggests, when using union types, thetypes don’t have to share a common hierarchy, and you can still accept them as arguments or return them from a method.

If you’re an application developer, you’ll use features like type inference every day and generics every week. When you read the Scaladoc for classes and methods, you’ll also need to have some understanding of variance. Hopefully you’ll see that using types can be relatively simple and also offers a lot of expressive power, flexibility, and control for library developers.

Benefits of types

Statically-typed programming languages offer a number of benefits, including:

  • Helping to provide strong IDE support
  • Eliminating many classes of potential errors at compile time
  • Assisting in refactoring
  • Providing strong documentation that cannot be outdated since it is type checked

Introducing features of Scala’s type system

Given that brief introduction, the following sections provide an overview of the features of Scala’s type system.

Contributors to this page: