0Pricing
Swift Academy · Lesson

Default Implementations in Extensions

Provide shared behavior via protocol extensions.

Default Implementations in Extensions is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Default Implementations in Extensions” lesson free?

Yes — the full text of “Default Implementations in Extensions” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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