Protocol extensions vs type extensions
Contrast protocol extensions (add default implementations to a protocol, even conditionally) with type extensions (add APIs to one concrete type). Understand dispatch behavior and constraints.
Overview
Goal: Know when to use a protocol extension (default behavior for all conformers) vs a type extension (APIs for one concrete type). We'll also note dispatch rules.
Protocol + conformance
Protocols declare requirements. Types conform by implementing those requirements.
protocol Greeter {
func greet() -> String
}
struct Person: Greeter {
var name: String
func greet() -> String { "Hi, I'm \\(name)" } // concrete requirement impl
}
print(Person(name: "Ada").greet())All lessons in this course
- Protocols & Extensions basics
- Protocol extensions vs type extensions
- Static vs instance requirements