Swift Academy · Lesson

Default Implementations in Extensions

Provide shared behavior via protocol extensions.

Lesson 2 of 413 steps

Default Implementations in Extensions is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

The Problem

Protocols declare requirements but provide no behavior. If every conforming type had to repeat the same logic, you would duplicate code. Protocol extensions solve this by supplying default implementations.

protocol Greeter {
    func greet() -> String
}

Adding a Default

An extension on the protocol can implement a requirement. Conforming types get that behavior for free unless they override it.

protocol Greeter { func greet() -> String }

extension Greeter {
    func greet() -> String { "Hello!" }
}

struct Robot: Greeter {}
print(Robot().greet())

Overriding the Default

A conforming type may provide its own implementation, which takes priority over the extension default.

protocol Greeter { func greet() -> String }
extension Greeter { func greet() -> String { "Hello!" } }

struct French: Greeter {
    func greet() -> String { "Bonjour!" }
}
print(French().greet())

Adding Bonus Methods

Extensions can add brand-new methods that are not in the protocol at all. Every conforming type gains them automatically.

protocol Named { var name: String { get } }

extension Named {
    func loudName() -> String { name.uppercased() }
}

struct City: Named { let name: String }
print(City(name: "paris").loudName())

Default Computed Properties

Extensions can also supply computed properties as defaults.

protocol Sized {
    var width: Double { get }
    var height: Double { get }
}

extension Sized {
    var area: Double { width * height }
}

struct Rect: Sized { let width: Double; let height: Double }
print(Rect(width: 2, height: 5).area)

Building on Requirements

A default implementation can call the protocol's own requirements, composing richer behavior from the minimal contract each type must supply.

protocol Summable {
    var values: [Int] { get }
}

extension Summable {
    var total: Int { values.reduce(0, +) }
}

struct Cart: Summable { let values: [Int] }
print(Cart(values: [10, 20, 30]).total)

Constrained Extensions

Use a where clause so a default applies only when extra conditions hold. Here the default exists only when Self is also Equatable.

protocol Container {
    associatedtype Item
    var items: [Item] { get }
}

extension Container where Item: Equatable {
    func contains(_ x: Item) -> Bool { items.contains(x) }
}

struct Bag: Container { let items: [Int] }
print(Bag(items: [1, 2, 3]).contains(2))

Default + Requirement Together

You can declare a method in the protocol AND provide a default. Listing it in the protocol enables dynamic dispatch through the protocol type.

protocol Logger {
    func log() -> String
}
extension Logger {
    func log() -> String { "default log" }
}

struct A: Logger {}
struct B: Logger { func log() -> String { "B log" } }

let loggers: [Logger] = [A(), B()]
for l in loggers { print(l.log()) }

Static Dispatch Gotcha

If a method is ONLY in the extension (not the protocol), calls through a protocol-typed value use the extension version, even if the concrete type defined its own. Declare it in the protocol for dynamic dispatch.

protocol P {}
extension P { func who() -> String { "P-default" } }
struct Q: P { func who() -> String { "Q" } }

let direct = Q()
let asP: P = Q()
print(direct.who())
print(asP.who())

Reducing Boilerplate

The combination of a tiny required surface plus rich defaults is the heart of protocol-oriented design: conforming types implement the minimum, and shared behavior lives in one place.

protocol Identifiable { var id: Int { get } }
extension Identifiable {
    var tag: String { "ID-" + String(id) }
}
struct Order: Identifiable { let id: Int }
print(Order(id: 7).tag)

Combining Multiple Extensions

You can split defaults across several extensions for organization; they all contribute to the conforming type.

protocol Money { var cents: Int { get } }
extension Money { var dollars: Double { Double(cents) / 100 } }
extension Money { var label: String { "$" + String(format: "%.2f", dollars) } }

struct Price: Money { let cents: Int }
print(Price(cents: 1299).label)

Quick Check

Test your grasp of protocol extension defaults.

Recap

Protocol extensions provide default implementations, letting conforming types inherit shared behavior and override when needed. You can add bonus methods and computed properties, constrain defaults with where, and you saw the static-vs-dynamic dispatch rule: declare a method in the protocol for dynamic dispatch.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Default Implementations in Extensions” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Default Implementations in Extensions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Default Implementations in Extensions”?

Provide shared behavior via protocol extensions. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Default Implementations in Extensions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Protocols as Contracts
  2. Default Implementations in Extensions
  3. Protocol Composition
  4. Conditional Conformance
← Back to Swift Academy