Inference with Literals
Control whether a literal becomes Int or Double.
Inference with Literals is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
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)let y: Double = 5
Annotating with Double lets the same literal 5 become a floating-point value.
let y: Double = 5
print(type(of: y))
print(y / 2)Decimal Point Forces Double
Writing 5.0 infers Double with no annotation needed.
let z = 5.0
print(type(of: z))
print(z * 2)Int and Double Do Not Mix
Swift will not implicitly convert between numeric types. You must convert explicitly to combine an Int and a Double.
let count = 3
let price = 1.5
let total = Double(count) * price
print(total)Float vs Double
The default for decimals is Double. Annotate with Float when you specifically want the smaller 32-bit type.
let big = 3.14
let small: Float = 3.14
print(type(of: big), type(of: small))Integer Type Annotations
Annotate to pick a sized integer such as Int8 or UInt instead of the default Int.
let tiny: Int8 = 120
let unsigned: UInt = 9
print(type(of: tiny), type(of: unsigned))String vs Character
A double-quoted literal infers String. A single-character context can infer Character with an annotation.
let word = "Swift"
let letter: Character = "S"
print(type(of: word), type(of: letter))Boolean Literals
The keywords true and false infer Bool.
let isReady = false
print(type(of: isReady))Literals in Expressions
When literals mix in one expression, Swift picks a common type. 1 + 2.0 treats the integer literal as a Double.
let mixed = 1 + 2.0
print(mixed)
print(type(of: mixed))Annotation Adapts the Literal
The literal itself is flexible; the annotation decides its final type. This is why let y: Double = 5 works without writing 5.0.
let a: Double = 7
let b: Float = 7
print(a, b)Quick Check
Test literal inference.
Recap
Integer literals default to Int, decimals to Double. Annotations like : Double, : Float, : Int8, or : Character override the default by adapting the flexible literal. Numeric types never convert implicitly.
let i = 5
let d: Double = 5
print(type(of: i), type(of: d))Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Inference with Literals” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Inference with Literals”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “Inference with Literals”?
Control whether a literal becomes Int or Double. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Inference with Literals” 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
- How Type Inference Works
- When to Add Explicit Annotations
- Inference with Literals
- Inference in Closures