Protocols: define a capability
Protocols: define a capability
A school needs to print labels for different kinds of objects. A protocol describes requirements a conforming type must provide, such as a property or method. It lets code depend on a capability without requiring every type to inherit from one common class.
What you’ll learn
- State a readable-property protocol requirement.
- Pass different conforming values through a capability-based function.
Protocols: define a capability in the example
Capability contract
A school needs to print labels for different kinds of objects.
Capability contractConforming value
Conformance means implementing the requirements under their access and type rules.
Conforming valueFunction uses the contract
The protocol requires a readable String label.
Function uses the contract
How protocols: define a capability works
Conformance means implementing the requirements under their access and type rules. Both structs and classes can conform. A parameter written any Protocol uses an existential value that can hold a conforming instance; generics offer another way to preserve more static type information.
Work through protocols: define a capability
Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.
Swift · complete program
protocol Labelled {
var label: String { get }
}
struct Student: Labelled {
let label: String
}
func display(_ item: any Labelled) { print(item.label) }
display(Student(label: "Diya"))Expected output
DiyaFollow the execution
The protocol requires a readable String label. Student’s stored property satisfies that requirement. Display accesses only the promised capability and does not need knowledge of student-specific fields.
Try this program in the Swift compiler. Paste the complete source above.
Practice: Protocols: define a capability
Your task: Could a Room struct with a String label also be passed to display?
Swift · complete program
protocol Labelled {
var label: String { get }
}
struct Student: Labelled {
let label: String
}
func display(_ item: any Labelled) { print(item.label) }
display(Student(label: "Diya"))Worked answer
Yes, if Room explicitly conforms to Labelled and satisfies the requirement. The function depends on the capability, not on the Student type or class inheritance.
Depend on the promised capability
A protocol is not an automatic implementation of all its requirements. Associated types, Self requirements and mutation introduce additional rules; use generic constraints when the relationship between types matters rather than treating every existential as interchangeable.
Check your decision
Depend on the promised capability
Yes, if Room explicitly conforms to Labelled and satisfies the requirement. The function depends on the capability, not on the Student type or class inheritance. A protocol is not an automatic implementation of all its requirements.
Further reading: official Swift language guide.
Reaching the recap and completing the quizzes are separate milestones.