Protocol composition lets you combine multiple protocols into one requirement. This helps you write flexible and clear code by saying "I need something that does all these things."
Protocol composition in practice in Swift
func exampleFunction(param: ProtocolA & ProtocolB) { // code using param }
Use the ampersand (&) to combine protocols.
The parameter must conform to all listed protocols.
protocol Drivable { func drive() } protocol Refuelable { func refuel() } func operate(vehicle: Drivable & Refuelable) { vehicle.drive() vehicle.refuel() }
protocol Readable { func read() } protocol Writable { func write() } func process(document: Readable & Writable) { document.read() document.write() }
This program defines two protocols, Painter and Cleaner. The Robot struct conforms to both. The function startWork requires a worker that can paint and clean. We create a Robot and pass it to startWork, so it paints and cleans.
protocol Painter { func paint() } protocol Cleaner { func clean() } struct Robot: Painter, Cleaner { func paint() { print("Painting the wall") } func clean() { print("Cleaning the floor") } } func startWork(worker: Painter & Cleaner) { worker.paint() worker.clean() } let robot = Robot() startWork(worker: robot)
Protocol composition is a way to require multiple behaviors without making a new protocol.
You can use protocol composition anywhere a type is expected, like function parameters or variables.
It helps keep your code flexible and easy to change later.
Protocol composition combines multiple protocols using &.
It lets you require multiple behaviors at once without new protocols.
Use it to write clear, flexible, and reusable code.
Practice
Solution
Step 1: Understand protocol composition concept
Protocol composition uses the & operator to require multiple protocols at once without defining a new protocol.Step 2: Compare options with concept
Only Combine multiple protocols into one requirement without creating a new protocol correctly describes combining multiple protocols without creating a new one. Other options describe unrelated or incorrect Swift features.Final Answer:
Combine multiple protocols into one requirement without creating a new protocol -> Option AQuick Check:
Protocol composition = Combine protocols [OK]
- Confusing protocol composition with class inheritance
- Thinking protocols can have stored properties
- Believing protocol extensions override methods
Codable and Equatable protocols using protocol composition?Solution
Step 1: Recall protocol composition syntax
Swift uses & to combine protocols in type requirements, likeCodable & Equatable.Step 2: Evaluate each option
func save(item: Codable & Equatable) {} uses correct & syntax. func save(item: Codable | Equatable) {} uses | which is invalid. func save(item: Codable, Equatable) {} uses comma which is invalid. func save(item: Codable && Equatable) {} uses && which is invalid.Final Answer:
func save(item: Codable & Equatable) {} -> Option BQuick Check:
Use & to combine protocols [OK]
- Using | or && instead of & for protocol composition
- Separating protocols with commas in parameter types
- Confusing logical operators with protocol composition
protocol A { func foo() }
protocol B { func bar() }
struct MyStruct: A, B {
func foo() { print("foo") }
func bar() { print("bar") }
}
func callBoth(item: A & B) {
item.foo()
item.bar()
}
let obj = MyStruct()
callBoth(item: obj)Solution
Step 1: Understand protocol composition usage
The functioncallBothrequires an argument conforming to bothAandB.MyStructconforms to both.Step 2: Trace function calls
InsidecallBoth,foo()prints "foo" andbar()prints "bar". Both are called in order.Final Answer:
foo bar -> Option AQuick Check:
Both protocol methods called = foo\nbar [OK]
- Assuming only one protocol method runs
- Expecting a compile error due to protocol composition
- Confusing protocol conformance with inheritance
protocol P1 { func p1Method() }
protocol P2 { func p2Method() }
func testFunc(param: P1 & P2) {
param.p1Method()
param.p2Method()
}
struct S: P1 {
func p1Method() { print("P1") }
}
let s = S()
testFunc(param: s)Solution
Step 1: Check struct conformance
StructSconforms only toP1, missingP2conformance.Step 2: Analyze function parameter requirement
FunctiontestFuncrequires a parameter conforming to bothP1andP2. Passingsfails becauseslacksP2.Final Answer:
Struct S does not conform to protocol P2 -> Option DQuick Check:
Missing protocol conformance causes error [OK]
- Assuming partial protocol conformance is enough
- Thinking function parameter syntax is incorrect
- Believing protocols cannot be combined with &
protocol Drawable { func draw() }
protocol Colorable { var color: String { get } }
struct Circle: Drawable, Colorable {
var color: String
func draw() { print("Drawing a circle") }
}
struct Square: Drawable {
func draw() { print("Drawing a square") }
}Which of the following function signatures correctly accepts only objects that are both
Drawable and Colorable?Solution
Step 1: Understand protocol composition for parameters
To accept only objects conforming to bothDrawableandColorable, useDrawable & Colorable.Step 2: Check each function signature
func render(item: Drawable & Colorable) { item.draw(); print(item.color) } uses correct composition and accesses bothdraw()andcolor. Options A, B, and D either use invalid syntax or accept only one protocol.Final Answer:
func render(item: Drawable & Colorable) { item.draw(); print(item.color) } -> Option CQuick Check:
Use & to require multiple protocols [OK]
- Using | instead of & for protocol composition
- Accepting only one protocol when both are needed
- Trying to access properties/methods not guaranteed by parameter type
Ready to prove it?
Pass the test and this pattern moves unknown
