0Pricing
Swift Academy · Lesson

Protocol extensions vs type extensions

Contrast protocol extensions (add default implementations to a protocol, even conditionally) with type extensions (add APIs to one concrete type). Understand dispatch behavior and constraints.

Protocol extensions vs type extensions is a free Swift Academy lesson on CoddyKit — lesson 2 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Overview

Goal: Know when to use a protocol extension (default behavior for all conformers) vs a type extension (APIs for one concrete type). We'll also note dispatch rules.

Protocol + conformance

Protocols declare requirements. Types conform by implementing those requirements.

protocol Greeter {
    func greet() -> String
}
struct Person: Greeter {
    var name: String
    func greet() -> String { "Hi, I'm \\(name)" }  // concrete requirement impl
}
print(Person(name: "Ada").greet())

Default impls via protocol ext

Protocol extension can provide default implementations used by all conforming types unless they provide their own.

protocol Describable {
    func describe() -> String
}
extension Describable {
    // default for ALL conformers until they override
    func describe() -> String { "No description" }
}

struct Car: Describable { }             // uses default
struct Book: Describable {
    func describe() -> String { "A book" } // custom override
}
print(Car().describe(), Book().describe())

Type extension APIs

Type extensions add methods/computed properties to a single concrete type. They don’t add stored properties.

struct Point { var x: Int; var y: Int }

extension Point {
    // add helper just to Point
    var lengthSquared: Int { x*x + y*y }
    func moved(dx: Int, dy: Int) -> Point { Point(x: x+dx, y: y+dy) }
}
let p = Point(x: 2, y: 3)
print(p.lengthSquared, p.moved(dx: 1, dy: -1))

Conditional extensions

Add behavior to some conforming types using a where clause on the protocol extension.

protocol Summable { }
extension Int: Summable { }
extension Array where Element: Summable {
    // only for arrays whose elements conform to Summable
    func joinedCount() -> Int { self.count }
}
print([1,2,3].joinedCount())   // works (Int conforms)
 // print(["a","b"].joinedCount()) // not available

Dispatch behavior (overview)

Rule of thumb: If a method is a protocol requirement and a conforming type implements it, that wins (dynamic-like). Methods added only in a protocol extension (not requirements) are statically dispatched.

protocol Speaker { func speak() -> String }
extension Speaker { func speak() -> String { "default" } }

struct Cat: Speaker { }
struct Dog: Speaker {
    func speak() -> String { "woof" } // requirement impl overrides default
}

let s1: Speaker = Cat()
let s2: Speaker = Dog()
print(s1.speak()) // "default" (uses default impl)
print(s2.speak()) // "woof" (Dog provides requirement impl)

let c = Cat()
print(c.speak())  // "default" via extension on protocol

Proto vs type extension

Quick check: What's the key difference?

Recap

Recap: Use protocol extensions for reusable defaults (optionally conditional). Use type extensions to grow one type. Remember dispatch: requirement overrides beat default extension methods.

Frequently asked questions

Is the “Protocol extensions vs type extensions” lesson free?

Yes — the full text of “Protocol extensions vs type extensions” is free to read here on the web, and the Swift Academy course includes 3 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 “Protocol extensions vs type extensions”?

Contrast protocol extensions (add default implementations to a protocol, even conditionally) with type extensions (add APIs to one concrete type). Understand dispatch behavior and constraints. 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 3, so you can start here or from the beginning and move at your own pace.

How long does the “Protocol extensions vs type extensions” 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. Protocols & Extensions basics
  2. Protocol extensions vs type extensions
  3. Static vs instance requirements
← Back to Swift Academy