9 of 22 · System architecture
Protocol-Oriented Programming
Protocol-oriented programming models shared behavior as protocols with default implementations in extensions, so value types gain behavior by conformance instead of inheriting from a base class.
How it's wired
- Protocols plus extensions give shared behavior to structs and enums.
- Generics (some P) keep it fast and type-safe.
| Part | Owns | Looks like |
|---|---|---|
| Protocol | Declare requirements | protocol ThanksFormatter |
| Extension | Default implementations | extension ThanksFormatter |
| Conformers | Value types adopting behavior | struct, enum |
| Generic use | Static dispatch at call sites | func render(_ f: some ThanksFormatter) |
Where it gets subtle
- Dispatch: a method declared only in an extension, not in the protocol, dispatches statically. Calling it through the protocol type ignores a conformer's own version. It's a classic bug.
- some P is one concrete type fixed at compile time: specialized and fast. any P is an existential box with dynamic dispatch and possible heap allocation. Default to some.
- Associated types make protocols powerful and awkward as values. Reach for generics or primary associated types (any Collection<Int>) before writing type erasers.
- Don't create a protocol per class "for testing". If there's one implementation and no seam is needed, skip the protocol.
When I reach for protocols
| A real seam | Swapping storage, network or clock in tests and features. |
|---|---|
| Shared behavior across unrelated types | Protocol plus extension defaults. |
| One implementation, no seam | A concrete type. No protocol. |
Compared
Inheritance vs some P vs any P
| Inheritance | some P | any P | |
|---|---|---|---|
| Dispatch | Dynamic (vtable) | Static when specialized, else witness table | Dynamic (witness table) |
| Mixed types in one array | Yes | No | Yes |
| Value types | No | Yes | Yes |
| Runtime cost | Reference semantics | Lowest | Box + indirection |
Expert notes
- Protocols are seams. Put them where you need to swap or test something, and nowhere else.
Check yourself
Answer out loud before you reveal. Recall is what makes it stick.
Explain Protocol-Oriented Programming in 30 seconds.
- Protocol-oriented programming puts shared behavior in protocols with default implementations in extensions, so value types gain behavior without a base class.
- Both sides depend on the abstraction, which creates seams for testing and swapping.
- The nuances: some P is one concrete type and fast, any P boxes it and dispatches dynamically, and a method only in an extension dispatches statically.
- I add protocols only where something actually varies.
some versus any: what's the real difference, and which should be your default?
- Default to generics or
some P, and useany Ponly when you need to mix concrete types at runtime. - An
any Pis an existential box. - It may heap-allocate, its calls can't be specialized, and protocols with associated types get awkward.
- The explicit
anykeyword exists to make that cost visible. - Use
somewhen one concrete type is fixed at compile time. someis fast because the compiler knows the concrete type, so it can specialize and inline calls.
A function returns a long nested generic type that leaks implementation details. How do you hide it without losing performance?
- Return an opaque type, like
func makeShape() -> some Shape. - Callers only see the protocol, but the compiler keeps the concrete type, so generic code still optimizes.
- That's the difference from an existential.
- The constraint is that every return path must produce the same concrete type.
- If branches differ, you need a type-erasing wrapper or a result builder.
A type implements a method that also lives in a protocol extension, yet the extension's version runs. Why?
- Only protocol requirements are dynamically dispatched.
- A method that exists only in a protocol extension is statically dispatched, so calling through the protocol type runs the extension's version and skips the type's own.
- Fix it by declaring the method in the protocol, like
protocol Greeter { func greet() }, and keeping the extension as the default implementation.