Inference with Literals
Control whether a literal becomes Int or Double.
Default Literal Types
Each literal kind has a default inferred type: integers become Int, floating-point becomes Double, text becomes String, and true/false becomes Bool.
let i = 5
let d = 5.0
let s = "5"
let b = true
print(type(of: i), type(of: d), type(of: s), type(of: b))let x = 5 Is Int
An integer literal without a decimal point always infers Int, the platform's default integer type.
let x = 5
print(type(of: x))
print(x + 1)All lessons in this course
- How Type Inference Works
- When to Add Explicit Annotations
- Inference with Literals
- Inference in Closures