0Pricing
Swift Academy · Lesson

Synthesized Equatable

Get == for free and customize when needed.

Synthesized Equatable is a free Swift Academy lesson on CoddyKit — lesson 1 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.

What Is Equatable?

Equatable is the protocol that gives a type the == operator. Conforming lets you compare two values for equality and use them with contains, firstIndex, and more.

Automatic Conformance

For a struct whose stored properties are all Equatable, Swift synthesizes == for you — just declare conformance:

struct Point: Equatable {
    let x: Int
    let y: Int
}
print(Point(x: 1, y: 2) == Point(x: 1, y: 2))  // true
print(Point(x: 1, y: 2) == Point(x: 9, y: 2))  // false

How Synthesis Works

The generated == compares every stored property pairwise. All must be equal for the values to be equal:

struct User: Equatable {
    let name: String
    let age: Int
}
let a = User(name: "Ann", age: 30)
let b = User(name: "Ann", age: 31)
print(a == b)  // false  (age differs)

Using == in Collections

Equatable unlocks many array operations:

struct Tag: Equatable { let name: String }
let tags = [Tag(name: "a"), Tag(name: "b")]
print(tags.contains(Tag(name: "b")))        // true
print(tags.firstIndex(of: Tag(name: "a"))!) // 0

Enums Are Equatable Too

Enums get synthesized Equatable as well. Enums without associated values are Equatable even without declaring it; with associated values, declare conformance and the values are compared too.

Equatable Enum with Associated Values

Associated values participate in the comparison:

enum Status: Equatable {
    case active
    case error(code: Int)
}
print(Status.error(code: 404) == Status.error(code: 404))  // true
print(Status.error(code: 404) == Status.error(code: 500))  // false

Nested Types

Synthesis composes: a struct containing Equatable members is itself synthesizable:

struct Line: Equatable {
    let start: Int
    let end: Int
}
struct Shape: Equatable {
    let lines: [Line]
}
print(Shape(lines: [Line(start: 0, end: 1)]) == Shape(lines: [Line(start: 0, end: 1)]))  // true

When Synthesis Fails

If any stored property is not Equatable, synthesis is unavailable and you must write == yourself (covered later):

struct Box: Equatable {
    let id: Int
    static func == (l: Box, r: Box) -> Bool { l.id == r.id }
}
print(Box(id: 1) == Box(id: 1))  // true

Classes Do Not Auto-Synthesize

Reference types do not get synthesized ==; you must implement it (comparing identity or fields):

final class Account: Equatable {
    let id: Int
    init(id: Int) { self.id = id }
    static func == (l: Account, r: Account) -> Bool { l.id == r.id }
}
print(Account(id: 7) == Account(id: 7))  // true

!= Comes for Free

Once == exists, != is automatically derived as its negation:

struct Color: Equatable { let hex: String }
print(Color(hex: "#fff") != Color(hex: "#000"))  // true

Equatable in Generic Code

Equatable lets generic functions compare elements — many algorithms require it:

func allSame<T: Equatable>(_ xs: [T]) -> Bool {
    guard let first = xs.first else { return true }
    return xs.allSatisfy { $0 == first }
}
print(allSame([2, 2, 2]))  // true
print(allSame(["a", "b"]))  // false

Quick Check

For which type does Swift automatically synthesize == when you declare Equatable?

Recap

You learned Equatable:

  • Equatable provides == (and free !=)
  • Structs & enums with Equatable members get == synthesized
  • Enables contains, firstIndex(of:), etc.
  • Classes and non-Equatable members require a manual ==

Next: Hashable.

Frequently asked questions

Is the “Synthesized Equatable” lesson free?

Yes — the full text of “Synthesized Equatable” 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 “Synthesized Equatable”?

Get == for free and customize when needed. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Synthesized Equatable” 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. Synthesized Equatable
  2. Hashable and hash(into:)
  3. Comparable and Sorting
  4. Custom Equality Semantics
← Back to Swift Academy