Scala 3 — Book

Inferred Types

Language

As with other statically typed programming languages, in Scala you can declare a type when creating a new variable:

val x: Int = 1
val y: Double = 1

In those examples the types are explicitly declared to be Int and Double, respectively. However, in Scala you generally don’t have to declare the type when defining value binders:

val a = 1
val b = List(1, 2, 3)
val m = Map(1 -> "one", 2 -> "two")

When you do this, Scala infers the types, as shown in the following REPL interaction:

scala> val a = 1
val a: Int = 1

scala> val b = List(1, 2, 3)
val b: List[Int] = List(1, 2, 3)

scala> val m = Map(1 -> "one", 2 -> "two")
val m: Map[Int, String] = Map(1 -> one, 2 -> two)

Indeed, most variables are defined this way, and Scala’s ability to automatically infer types is one feature that makes it feel like a dynamically typed language.

Contributors to this page: