0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Type Inference

Let the compiler figure out types.

What Is Type Inference?

Scala is statically typed, yet you rarely have to write types out. The compiler infers them from the value on the right-hand side.

This gives you the safety of types with the brevity of a dynamic language.

val n = 42        // inferred Int
val label = "hi" // inferred String

Inferring From Literals

When you write val x = 5, the compiler sees the Int literal and gives x the type Int.

A decimal literal becomes a Double, and quoted text becomes a String.

object Main extends App {
  val count = 5
  val ratio = 1.5
  println(count + ratio)
}

All lessons in this course

  1. val vs var
  2. Basic Types and Literals
  3. Type Inference
  4. Expressions over Statements
← Back to Scala for Backend Engineering & Functional Programming