Protocol Composition
Combine multiple protocol requirements with &.
Protocol Composition is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Combining Protocols
Sometimes a value must satisfy several protocols at once. Swift lets you compose protocols with the & operator: SomeProtocol & AnotherProtocol.
protocol Named { var name: String { get } }
protocol Aged { var age: Int { get } }A Composed Parameter
A parameter typed Named & Aged accepts any value that conforms to both protocols.
protocol Named { var name: String { get } }
protocol Aged { var age: Int { get } }
struct Person: Named, Aged {
let name: String
let age: Int
}
func intro(_ x: Named & Aged) {
print(x.name + " is " + String(x.age))
}
intro(Person(name: "Sam", age: 28))Why Not Inheritance?
Composition lets you require both protocols at the call site without creating a new combined protocol. It keeps protocols small and reusable.
protocol Drawable { func draw() -> String }
protocol Resizable { func resize(_ f: Double) -> String }
struct Icon: Drawable, Resizable {
func draw() -> String { "drawing" }
func resize(_ f: Double) -> String { "x" + String(f) }
}
func render(_ v: Drawable & Resizable) {
print(v.draw(), v.resize(2))
}
render(Icon())Three or More
You can chain as many protocols as needed with additional & operators.
protocol A { func a() -> String }
protocol B { func b() -> String }
protocol C { func c() -> String }
struct X: A, B, C {
func a() -> String { "a" }
func b() -> String { "b" }
func c() -> String { "c" }
}
func use(_ v: A & B & C) { print(v.a(), v.b(), v.c()) }
use(X())Composition with a Class
You can combine a class type with protocols: SomeClass & SomeProtocol requires the value be that class (or subclass) AND conform to the protocol.
class Vehicle { var speed = 0 }
protocol Electric { var battery: Int { get } }
class Tesla: Vehicle, Electric { var battery = 100 }
func check(_ v: Vehicle & Electric) {
print(v.speed, v.battery)
}
check(Tesla())Returning a Composition
A function can return a composed type, promising the caller that the result conforms to all listed protocols.
protocol Named { var name: String { get } }
protocol Aged { var age: Int { get } }
struct Person: Named, Aged { let name: String; let age: Int }
func makePerson() -> Named & Aged {
Person(name: "Eve", age: 22)
}
let p = makePerson()
print(p.name, p.age)typealias for Composition
Give a composition a readable name with typealias so you can reuse it without repeating the & chain.
protocol Codable2 { func encode() -> String }
protocol Loggable { func log() -> String }
typealias Persistable = Codable2 & Loggable
struct Record: Persistable {
func encode() -> String { "data" }
func log() -> String { "logged" }
}
func save(_ r: Persistable) { print(r.encode(), r.log()) }
save(Record())Composition in Collections
You can store composed-type values in arrays, accessing all protocols' members on each element.
protocol Named { var name: String { get } }
protocol Aged { var age: Int { get } }
struct P: Named, Aged { let name: String; let age: Int }
let group: [Named & Aged] = [P(name: "A", age: 1), P(name: "B", age: 2)]
for g in group { print(g.name, g.age) }Composition with Standard Protocols
Composition shines with library protocols. For example require both CustomStringConvertible and Equatable.
func report<T>(_ items: [T]) where T: CustomStringConvertible & Equatable {
for i in items { print(i.description) }
}
report([1, 2, 3])Empty Protocols as Markers
Composition can combine a marker protocol (no requirements) with a functional one to express intent in the type system.
protocol Secure {}
protocol Sendable2 { func send() -> String }
struct Packet: Secure, Sendable2 {
func send() -> String { "sent securely" }
}
func transmit(_ p: Secure & Sendable2) { print(p.send()) }
transmit(Packet())Composition vs Generics
Composition (A & B) erases the concrete type; generics (<T: A & B>) preserve it. Use generics when you need to relate parameter and return types.
protocol A { func a() -> String }
protocol B { func b() -> String }
struct S: A, B { func a() -> String { "a" }; func b() -> String { "b" } }
func generic<T: A & B>(_ x: T) -> T { print(x.a(), x.b()); return x }
_ = generic(S())Quick Check
Test your understanding of protocol composition.
Recap
Protocol composition with & lets you require conformance to multiple protocols (and optionally one class) at once, without defining a new combined protocol. You can name compositions with typealias, use them in parameters, returns, and collections, and you saw how composition differs from generics in preserving concrete types.
Frequently asked questions
Is the “Protocol Composition” lesson free?
Yes — the full text of “Protocol Composition” is free to read here on the web, and the Swift Academy course includes 4 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 “Protocol Composition”?
Combine multiple protocol requirements with &. 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 4, so you can start here or from the beginning and move at your own pace.
How long does the “Protocol Composition” 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.