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 ).
where clauses on extensions is a free Swift Academy lesson on CoddyKit — lesson 3 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.
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"])) // trueProtocol 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) // falseExact-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.0Capability-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) // 6Design 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.
Frequently asked questions
Is the “where clauses on extensions” lesson free?
Yes — the full text of “where clauses on 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 “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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
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
- Conditional conformances
- Recursive constraints & higher-order generics
- where clauses on extensions