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