Type Constraints
A type constraint is a rule that says a generic placeholder can only be filled in by types that meet a certain requirement, like "must be comparable."
In this page:
Constraining to Comparable
Adding : Comparable to a generic placeholder allows the function to use comparison operators like < and >, which aren't guaranteed on an unconstrained type.
Example: Constraining to Comparable
func findLargest<T: Comparable>(_ items: [T]) -> T? {
guard var largest = items.first else { return nil }
for item in items where item > largest {
largest = item
}
return largest
}
print(findLargest([3, 7, 2, 9, 4]) as Any)
print(findLargest(["banana", "apple", "cherry"]) as Any)
Login to try C/C++/Java/PHP code in the editor
Constraining to a Custom Protocol
A generic placeholder can also be constrained to any custom protocol, requiring conforming types to implement its specific requirements.
Example: Constraining to a Custom Protocol
protocol Summable {
static func + (lhs: Self, rhs: Self) -> Self
static var zero: Self { get }
}
extension Int: Summable {
static var zero: Int { 0 }
}
func total<T: Summable>(_ items: [T]) -> T {
var result = T.zero
for item in items {
result = result + item
}
return result
}
print(total([1, 2, 3, 4]))
Login to try C/C++/Java/PHP code in the editor
Common Mistakes
- Writing a generic function that uses
<or==on its placeholder type without constraining it toComparableorEquatablefirst -- the compiler will reject unconstrained comparisons. - Confusing a class-inheritance constraint (
T: SomeClass) with a protocol constraint (T: SomeProtocol); both use the same colon syntax but mean different things. - Over-constraining a generic function with requirements it doesn't actually need, unnecessarily limiting what types can be used.
Chapter Summary
- A constraint restricts a generic placeholder to types conforming to a protocol or inheriting a class.
- Constraints are written after the placeholder name with a colon, like
<T: Comparable>. - Constraints let you use operations (like
<or==) that aren't available on every possible type. - Multiple constraints can be combined using
&or awhereclause.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: