#69: Protocol Extension Dispatch Gotchas

There is one subtle detail about protocol extensions: methods declared only in the extension are not requirements of the protocol itself.

protocol Greeter {
    func greet()
}

extension Greeter {
    func greet() { print("Hello") }
    func wave() { print("👋") }
}

If you call wave() through a protocol-typed value, Swift uses the extension implementation, not some override-like dynamic lookup.

This catches people by surprise because protocol extensions can look a bit like inheritance, but the dispatch rules are different.


I'm also on Twitter and GitHub.

Subscribe via RSS.