Synthesized Equatable
Get == for free and customize when needed.
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)) // falseAll lessons in this course
- Synthesized Equatable
- Hashable and hash(into:)
- Comparable and Sorting
- Custom Equality Semantics