An Overview Of Scala Type Inference

Let’s review the syntax for declaring immutable variables in Scala.


val <Name of our variable> : <Scala type> = <Some literal>

As an example, you can define an immutable variable named donutsToBuy of type Int and assign its value to 5.


println("Step 1: Immutable variable")
val donutsToBuy: Int = 5

However, through type inference, Scala complier is smart enough to figure out that the literal 5 is actually an Integer.
As a result, you can simplify the declaration of some immutable variable of type Int as follows:


val donutsBought = 5