0Pricing
Swift Academy · Lesson

How Type Inference Works

See how Swift deduces types from context.

What Is Type Inference

Swift can deduce the type of a value from the context of an expression. You often do not need to write the type explicitly.

let count = 42
let name = "Swift"
print(type(of: count))
print(type(of: name))

Inferred From Literals

An integer literal infers Int, a decimal literal infers Double, and a quoted literal infers String.

let a = 10
let b = 3.14
let c = "hello"
print(type(of: a), type(of: b), type(of: c))

All lessons in this course

  1. How Type Inference Works
  2. When to Add Explicit Annotations
  3. Inference with Literals
  4. Inference in Closures
← Back to Swift Academy