0Pricing
Swift Academy · Lesson

Type Inference and Explicit Types

How Swift infers types and when to declare them explicitly.

Type Inference and Explicit Types is a free Swift Academy lesson on CoddyKit — lesson 2 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.

Welcome

Swift is a *statically typed* language — every value has a type known at compile time. But you rarely need to write the type yourself because Swift infers it for you.

What is Type Inference?

Swift examines the value on the right-hand side and deduces the type automatically: ```swift let message = "Hello" // inferred as String let count = 42 // inferred as Int let price = 9.99 // inferred as Double ``` No type annotation needed — the compiler figures it out.

Explicit Type Annotations

You can always state the type explicitly using a colon: ```swift let message: String = "Hello" let count: Int = 42 let price: Double = 9.99 ``` This is useful when the inferred type wouldn't be what you want.

When Inference Picks the Wrong Type

Floating-point literals default to `Double`. If you need `Float`, annotate explicitly: ```swift let inferred = 3.14 // Double let explicit: Float = 3.14 // Float ``` Similarly, integer literals default to `Int` — annotate for `Int32`, `UInt8`, etc.

Type Safety in Practice

Once a type is set, it cannot change: ```swift var score = 0 // Int score = "ten" // ❌ cannot assign String to Int ``` Type safety catches this class of bug at compile time, long before runtime.

The type(of:) Function

Use `type(of:)` to inspect the runtime type: ```swift let value = 42 print(type(of: value)) // Int let name = "Alice" print(type(of: name)) // String ``` Useful for debugging type-inference surprises.

Type Aliases

`typealias` gives an existing type a new name to improve readability: ```swift typealias Celsius = Double typealias UserID = Int var temperature: Celsius = 36.6 var userId: UserID = 1001 ``` Type aliases are purely cosmetic — the underlying type is unchanged.

Annotations Without Initialisation

When you declare a variable without an initial value, you *must* annotate the type: ```swift var name: String // ✓ annotation required name = "Bob" var total: Int total = 0 ``` Swift has no way to infer a type without a value present.

Numeric Literals and Readability

Swift allows underscores in numeric literals for readability: ```swift let million = 1_000_000 let hex = 0xFF_EC_D3 let binary = 0b1111_0000 ``` The underscores are ignored by the compiler — purely cosmetic.

Type Conversion — No Implicit Casts

Swift never implicitly converts between types. You must do it explicitly: ```swift let intVal: Int = 5 let dblVal: Double = Double(intVal) // explicit conversion let sum = Double(intVal) + 2.5 ``` This avoids subtle bugs from unintended numeric promotions.

Quick Check

What type does Swift infer for `let x = 3.14`?

Recap

Key takeaways: • Swift infers types from initial values — no annotation usually needed • Explicit annotations (`: Type`) override inference • Floating-point defaults to `Double`, integers to `Int` • No implicit conversions — use `Int()`, `Double()`, etc. • `type(of:)` reveals the runtime type Next: the core types — numbers, Bool and Character.

Frequently asked questions

Is the “Type Inference and Explicit Types” lesson free?

Yes — the full text of “Type Inference and Explicit Types” 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 “Type Inference and Explicit Types”?

How Swift infers types and when to declare them explicitly. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Type Inference and Explicit Types” 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. let vs var: Constants and Variables
  2. Type Inference and Explicit Types
  3. Numbers, Booleans and Characters
  4. String Fundamentals and Interpolation
← Back to Swift Academy