Synthesized Equatable
Get == for free and customize when needed.
Synthesized Equatable is a free Swift Academy lesson on CoddyKit. This is lesson 1 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.
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)) // falseHow 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"))!) // 0Enums 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)) // falseNested 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)])) // trueWhen 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)) // trueClasses 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")) // trueEquatable 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"])) // falseQuick Check
For which type does Swift automatically synthesize == when you declare Equatable?
Recap
You learned Equatable:
Equatableprovides==(and free!=)- Structs & enums with Equatable members get
==synthesized - Enables
contains,firstIndex(of:), etc. - Classes and non-Equatable members require a manual
==
Next: Hashable.
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 “Synthesized Equatable” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Synthesized Equatable”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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
- Synthesized Equatable
- Hashable and hash(into:)
- Comparable and Sorting
- Custom Equality Semantics