Swift Academy · Lesson

where clauses on extensions

Use where on type and protocol extensions to add APIs only when constraints hold (e.g., Array where Element: Equatable , Collection where Element == Int ).

Lesson 3 of 38 steps

where clauses on extensions is a free Swift Academy lesson on CoddyKit. This is lesson 3 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.

Why constrained extensions?

Add APIs only when types meet constraints using where on extensions:

  • Type extensions (e.g., Array)
  • Protocol extensions (e.g., Collection)
  • Exact-match constraints (e.g., Element == Int)

Type extension with constraint

Attach methods to Array only when its Element is Equatable. Non-Equatable arrays won’t see these APIs.

extension Array where Element: Equatable {
    func removing(_ value: Element) -> [Element] {
        filter { $0 != value }
    }
    func containsAll(_ others: [Element]) -> Bool {
        others.allSatisfy(self.contains)
    }
}
print([1,2,3].removing(2))        // [1,3]
print(["a","b"].containsAll(["b"])) // true

Protocol extension with where

A protocol extension adds an API to all conforming types when the constraint holds (any Collection of Comparable elements here).

extension Collection where Element: Comparable {
    var isSorted: Bool {
        // Pair each element with the next; ensure non-decreasing order
        zip(self, dropFirst()).allSatisfy(<=)
    }
}
print([1,2,3].isSorted)     // true
print([3,1,2].isSorted)     // false

Exact-match constraints

Use equality constraints for targeted APIs, e.g., only when Element == Int. These won’t appear for other element types.

extension Collection where Element == Int {
    var sum: Int { reduce(0, +) }
    func average() -> Double {
        isEmpty ? 0 : Double(sum) / Double(count)
    }
}
print([1,2,3].sum)          // 6
print([1,2,3].average())    // 2.0

Capability-based APIs

Constrain by capability (protocols like Numeric, Hashable) to compose powerful, reusable helpers.

extension Collection where Element: Numeric {
    var total: Element { reduce(0, +) }
}
extension Sequence where Element: Hashable {
    var uniques: [Element] {
        var seen = Set<Element>()
        return filter { seen.insert($0).inserted }
    }
}
print([1,2,2,3].uniques)    // [1,2,3]
print([1,2,3].total)        // 6

Design notes

Tips:

  • Prefer minimal constraints for wider reuse.
  • Avoid overlapping extensions that define the same member; it can confuse overload resolution.
  • Document assumptions (ordering, uniqueness, numeric domains).

Constrained extension purpose

Quick check: What do constrained extensions let you do?

Recap

Recap: Use where on type/protocol extensions to target APIs precisely—by capability (: Comparable) or exact type (== Int)—keeping code safe and discoverable.

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 “where clauses on extensions” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “where clauses on 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 “where clauses on extensions”?

Use where on type and protocol extensions to add APIs only when constraints hold (e.g., Array where Element: Equatable , Collection where Element == Int ). 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 3.

How long does the “where clauses on 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. Conditional conformances
  2. Recursive constraints & higher-order generics
  3. where clauses on extensions
← Back to Swift Academy