ProgramGuru

Extensions and shared implementations

Extensions and shared implementations

Sometimes a type already contains the right data but needs another useful operation. An extension adds methods, computed properties or conformances without rewriting the original declaration. It helps group behaviour by purpose rather than forcing every feature into one large type body.

What you’ll learn

  • Add a computed operation with an extension.
  • Explain why an extension cannot add stored instance fields.

Extensions and shared implementations in the example

  1. Existing stored data

    Sometimes a type already contains the right data but needs another useful operation.

    Existing stored data
  2. Added computed behaviour

    An extension cannot add a new stored instance property.

    Added computed behaviour
  3. Derived greeting

    The extension computes a greeting from the existing stored name.

    Derived greeting
The extension computes a greeting from the existing stored name. Creating Student still initializes only name. Accessing greeting produces Welcome, Diya without adding separately stored text that could become stale.

How extensions and shared implementations works

An extension cannot add a new stored instance property. Protocol extensions can provide default implementations, but dispatch depends on whether a member is a protocol requirement and the value’s static view. Declare required behaviour in the protocol when conformers must provide customizable implementations.

Work through extensions and shared implementations

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

struct Student { let name: String }
extension Student {
  var greeting: String { "Welcome, \(name)" }
}
print(Student(name: "Diya").greeting)

Expected output

Welcome, Diya

Follow the execution

The extension computes a greeting from the existing stored name. Creating Student still initializes only name. Accessing greeting produces Welcome, Diya without adding separately stored text that could become stale.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Extensions and shared implementations

Your task: Could this extension add a stored attendance counter?

Swift · complete program

struct Student { let name: String }
extension Student {
  var greeting: String { "Welcome, \(name)" }
}
print(Student(name: "Diya").greeting)

Worked answer

No. Add storage to the original type or another appropriate owned object. Extensions can add computed behaviour, not new stored instance fields.

Add behaviour without new instance storage

Extending a type you do not own can make method naming and future compatibility difficult. Keep added behaviour coherent, avoid unrelated conveniences on every type and distinguish extension defaults from superclass overrides.

Check your decision

Could this extension add a stored attendance counter?
Choose one answer. You can try again.

Answer and explanation

No.

No. Add storage to the original type or another appropriate owned object. Extensions can add computed behaviour, not new stored instance fields.

Add behaviour without new instance storage

No. Add storage to the original type or another appropriate owned object. Extensions can add computed behaviour, not new stored instance fields. Extending a type you do not own can make method naming and future compatibility difficult.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Generics: reuse an algorithm while keeping types →