0Pricing
Swift Academy · Lesson

When to Add Explicit Annotations

Annotate to disambiguate and document intent.

Why Annotate

Even though inference is powerful, explicit type annotations improve clarity, resolve ambiguity, and document intent.

let temperature: Double = 36
print(temperature)
print(type(of: temperature))

Forcing a Double

Without an annotation, let x = 5 is an Int. Annotate to make it a Double when you need fractional math later.

let count: Double = 5
let average = count / 2
print(average)

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