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
- How Type Inference Works
- When to Add Explicit Annotations
- Inference with Literals
- Inference in Closures