Conditional conformances
Make a generic type conform to a protocol only if its type parameter meets constraints (e.g., Box<T>: Equatable where T: Equatable ).
Why conditional conformance?
Conditional conformance adds a protocol conformance only when type parameters meet constraints. This keeps APIs precise, avoids boilerplate, and composes powerfully with the standard library.
Generic type baseline
Start with a generic type. Without extra work, Box does not conform to Equatable/Hashable even if T does.
struct Box<T> {
var value: T
}
let bi = Box(value: 3)
let bs = Box(value: "hi")
// At this point Box has no Equatable/Hashable, regardless of T.All lessons in this course
- Conditional conformances
- Recursive constraints & higher-order generics
- where clauses on extensions