0Pricing
Swift Academy · Lesson

Self Requirements and Equatable Design

Protocols that use Self and how Equatable/Comparable leverage them.

Self Requirements and Equatable Design is a free Swift Academy lesson on CoddyKit — lesson 3 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.

Welcome

`Self` in a protocol refers to the concrete type that conforms. It enables protocols like `Equatable` and `Comparable` to work with heterogeneous types safely.

Self as Parameter Type

```swift protocol Equatable { static func == (lhs: Self, rhs: Self) -> Bool } ``` `Self` means both parameters must be the *same* concrete type. You can't compare an `Int` with a `String` through this protocol.

Conforming to Equatable

```swift struct Point: Equatable { var x: Double var y: Double // Auto-synthesised: // static func == (lhs: Point, rhs: Point) -> Bool // { lhs.x == rhs.x && lhs.y == rhs.y } } print(Point(x:1,y:2) == Point(x:1,y:2)) // true ```

Comparable with Self

```swift struct Version: Comparable { let major, minor, patch: Int static func < (lhs: Version, rhs: Version) -> Bool { (lhs.major, lhs.minor, lhs.patch) < (rhs.major, rhs.minor, rhs.patch) } } let v1 = Version(major:1,minor:2,patch:0) let v2 = Version(major:2,minor:0,patch:0) print(v1 < v2) // true ```

Self as Return Type

```swift protocol Clonable { func clone() -> Self } class Animal: Clonable { var name: String = "" required init() {} func clone() -> Self { type(of: self).init() } } ``` `Self` in the return type means subclasses return their own type, not `Animal`.

Protocol with Self Requirement (PAT)

```swift protocol Scalable { func scaled(by factor: Double) -> Self } struct Size: Scalable { var w, h: Double func scaled(by f: Double) -> Size { Size(w: w*f, h: h*f) } } ```

Hashable: Self + Equatable

```swift struct Color: Hashable { var r, g, b: UInt8 // Auto-synthesised: func hash(into:) and == } var palette: Set = [] palette.insert(Color(r:255,g:0,b:0)) ``` `Hashable` requires `Equatable` — both are synthesised automatically for simple structs.

Protocols with Self Cannot Be Used as Existentials Directly

```swift // Before Swift 5.7: var shapes: [Equatable] = [] // ❌ Equatable has Self requirement // After Swift 5.7 with 'any': var boxes: [any Equatable] = [] // ⚠️ limited API ``` The `any` keyword explicitly opts into existential boxing.

Synthesis Prerequisites

Auto-synthesis of `Equatable`/`Hashable`/`Comparable` requires: • All stored properties conform to the same protocol • Enum cases (including associated values) conform ```swift struct Pair: Equatable { var a, b: T } // Equatable synthesised automatically ```

Custom Equatable Override

```swift struct Person: Equatable { var id: UUID var name: String // Only compare by id, ignore name: static func == (lhs: Person, rhs: Person) -> Bool { lhs.id == rhs.id } } ```

Quick Check

In a protocol, what does `Self` refer to?

Recap

Key takeaways: • `Self` = the concrete conforming type • `Equatable`, `Comparable`, `Hashable` all use `Self` • Swift auto-synthesises these for simple structs and enums • `Self` in return types gives type-preserving APIs • Protocols with Self requirements need `any` for existentials (Swift 5.7+) Next: protocol-oriented design principles.

Frequently asked questions

Is the “Self Requirements and Equatable Design” lesson free?

Yes — the full text of “Self Requirements and Equatable Design” 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 “Self Requirements and Equatable Design”?

Protocols that use Self and how Equatable/Comparable leverage them. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Self Requirements and Equatable Design” 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. Protocol Composition with &
  2. Conditional Conformance
  3. Self Requirements and Equatable Design
  4. Protocol-Oriented Design Principles
← Back to Swift Academy