0Pricing
Swift Academy · Lesson

How Type Inference Works

See how Swift deduces types from context.

How Type Inference Works is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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))

Inferred From Initializer

When you call an initializer, the result type drives inference for the variable.

let pi = Double.pi
let flag = Bool(true)
print(type(of: pi))
print(type(of: flag))

Inferred From Function Return

A variable assigned from a function call takes the function's return type.

func makeGreeting() -> String {
    return "Hi"
}
let g = makeGreeting()
print(type(of: g))

Inference in Collections

Array and dictionary literals infer their element types from the values inside.

let nums = [1, 2, 3]
let pairs = ["a": 1, "b": 2]
print(type(of: nums))
print(type(of: pairs))

Bidirectional Inference

Swift infers in both directions: from the value to the variable, and from a declared type back into a literal.

let x = 5            // Int inferred from literal
let y: Double = 5   // literal adapts to Double
print(type(of: x), type(of: y))

Inference Across Expressions

The compiler solves the types of a whole expression together, picking types that make all operations valid.

let result = 2 + 3 * 4
print(result)
print(type(of: result))

Inference Reduces Noise

Inference keeps code clean. Writing let names: [String] = [...] is redundant when the literal already says String.

let names = ["Ada", "Bob", "Cleo"]
print(names.count)

When Inference Fails

If the compiler cannot find a single type, you get an error and must annotate. Empty collections are a classic case.

let empty: [Int] = []
print(empty.isEmpty)

Inference Is Static

Inference happens at compile time. Once inferred, the type is fixed; Swift is not dynamically typed.

var n = 10
// n = "text" // would be a compile error
n = 20
print(n)

Inference and Performance

Inference has no runtime cost. The compiler resolves types before producing the binary, so inferred code runs exactly as fast as annotated code.

let total = (1...100).reduce(0, +)
print(total)

Quick Check

Test your grasp of inference.

Recap

Type inference lets Swift deduce types from literals, initializers, return values, and surrounding context at compile time. It reduces noise with zero runtime cost, but fails when no single type can be determined (e.g. empty collections).

let a = 1
let b = 2.0
let c = "x"
print(type(of: a), type(of: b), type(of: c))

Frequently asked questions

Is the “How Type Inference Works” lesson free?

Yes — the full text of “How Type Inference Works” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “How Type Inference Works”?

See how Swift deduces types from context. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “How Type Inference Works” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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