0Pricing
Swift Academy · Lesson

Inference with Literals

Control whether a literal becomes Int or Double.

Inference with Literals is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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))

Frequently asked questions

Is the “Inference with Literals” lesson free?

Yes — the full text of “Inference with Literals” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. How Type Inference Works
  2. When to Add Explicit Annotations
  3. Inference with Literals
  4. Inference in Closures
← Back to Swift Academy