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.

Lesson 2 of 38 steps

Protocol extensions vs type extensions is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 3. 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 3 lessons in total.

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.

Free to start

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 “Protocol extensions vs type extensions” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Protocol extensions vs type extensions”, 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 3 lessons in total.

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

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