0
0
Swiftprogramming~10 mins

Protocol composition in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Protocol composition
Define Protocol A
Define Protocol B
Create variable with Protocol A & B
Assign instance conforming to both
Use combined protocol methods/properties
Protocol composition lets you combine multiple protocols into one type requirement, so a variable must conform to all listed protocols.
Execution Sample
Swift
protocol A {
    func foo()
}
protocol B {
    func bar()
}
struct S: A, B {
    func foo() { print("foo") }
    func bar() { print("bar") }
}
let x: A & B = S()
x.foo()
x.bar()
This code defines two protocols A and B, a struct S that conforms to both, then uses a variable x typed as A & B to call both methods.
Execution Table
StepActionEvaluationResult
1Define protocol A with method foo()protocol A createdA protocol exists
2Define protocol B with method bar()protocol B createdB protocol exists
3Define struct S conforming to A and BS has foo() and bar()S type ready
4Create variable x of type A & Bx can hold any instance conforming to bothx declared
5Assign S() instance to xS conforms to A and Bx = S()
6Call x.foo()Calls foo() from SOutput: foo
7Call x.bar()Calls bar() from SOutput: bar
8End of executionNo more codeProgram ends
💡 All steps executed; program ends after calling both methods on x
Variable Tracker
VariableStartAfter Step 5After Step 6After Step 7Final
xundefinedS instance assignedS instance (foo called)S instance (bar called)S instance
Key Moments - 3 Insights
Why can variable x call both foo() and bar() methods?
Because x is declared as type A & B, it must conform to both protocols, so it can call methods from both. See execution_table steps 4-7.
Can x hold an instance that conforms to only one protocol?
No, x requires an instance conforming to both A and B protocols. Step 5 shows assignment of S which conforms to both.
What happens if struct S only conforms to one protocol?
Assignment to x would fail because x expects both protocols. This is why S must conform to both, as in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of variable x after step 5?
AAn instance conforming only to protocol A
BUndefined
CAn instance of struct S
DAn instance conforming only to protocol B
💡 Hint
Check variable_tracker column 'After Step 5' and execution_table step 5
At which step does the program call the bar() method?
AStep 7
BStep 6
CStep 4
DStep 3
💡 Hint
Look at execution_table rows describing method calls
If struct S did not conform to protocol B, what would happen at step 5?
AAssignment would succeed
BAssignment to x would fail
Cx could call bar() method
DProgram would print 'bar'
💡 Hint
Refer to key_moments about protocol conformance and execution_table step 5
Concept Snapshot
Protocol composition syntax: let x: ProtocolA & ProtocolB
- Variable x must conform to all protocols combined
- Allows calling methods/properties from all protocols
- Useful to require multiple behaviors in one variable
Full Transcript
This example shows how to combine two protocols A and B using protocol composition in Swift. We define protocols A and B, each with one method. Then we create a struct S that conforms to both protocols by implementing both methods. We declare a variable x with type A & B, meaning it must conform to both protocols. We assign an instance of S to x. Then we call both methods foo() and bar() on x, which works because S implements both. The execution table traces each step from defining protocols, creating struct, declaring variable, assigning instance, and calling methods. The variable tracker shows how x changes from undefined to holding an instance of S. Key moments clarify why x can call both methods and why S must conform to both protocols. The quiz tests understanding of variable state and method calls during execution. The snapshot summarizes protocol composition usage in Swift.