#68: Default Implementations in Protocol Extensions

Protocol extensions can provide concrete implementations, which makes them a lightweight alternative to base classes for shared behavior.

protocol Resettable {
    mutating func reset()
}

extension Resettable {
    mutating func reset() {}
}

Conforming types can use the default implementation or provide their own.

This pattern is one of the most useful pieces of Swift’s protocol-oriented style.


I'm also on Twitter and GitHub.

Subscribe via RSS.