0
0
Swiftprogramming~10 mins

Equatable, Hashable, Comparable protocols in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Equatable, Hashable, Comparable protocols
Start
Define struct with protocols
Implement Equatable: ==
Implement Hashable: hash(into:)
Implement Comparable: <
Use instances in collections or compare
End
This flow shows how a Swift struct adopts Equatable, Hashable, and Comparable by implementing required methods, then uses instances for equality, hashing, and ordering.
Execution Sample
Swift
struct Person: Equatable, Hashable, Comparable {
    let name: String
    let age: Int

    static func == (lhs: Person, rhs: Person) -> Bool {
        lhs.name == rhs.name && lhs.age == rhs.age
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(name)
        hasher.combine(age)
    }

    static func < (lhs: Person, rhs: Person) -> Bool {
        lhs.age < rhs.age
    }
}

let p1 = Person(name: "Alice", age: 30)
let p2 = Person(name: "Bob", age: 25)
print(p1 == p2)
print(p1 < p2)
Defines a Person struct that can be compared for equality, hashed, and ordered by age, then compares two instances.
Execution Table
StepActionEvaluationResult
1Create p1Person(name: "Alice", age: 30)p1 = Person(name: "Alice", age: 30)
2Create p2Person(name: "Bob", age: 25)p2 = Person(name: "Bob", age: 25)
3Check p1 == p2p1.name == p2.name && p1.age == p2.agefalse ("Alice" != "Bob")
4Check p1 < p2p1.age < p2.agefalse (30 < 25 is false)
5Hash p1Combine name and agehash value computed
6Hash p2Combine name and agehash value computed
7EndNo more actionsProgram ends
💡 All comparisons and hashing done; program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
p1nilPerson(name: "Alice", age: 30)Person(name: "Alice", age: 30)Person(name: "Alice", age: 30)
p2nilnilPerson(name: "Bob", age: 25)Person(name: "Bob", age: 25)
Key Moments - 3 Insights
Why does p1 == p2 return false even if they are both Person?
Because the == operator compares both name and age (see step 3 in execution_table). Since names differ, the result is false.
How does the < operator decide which Person is smaller?
It compares the age property only (step 4). p1.age is 30, p2.age is 25, so p1 < p2 is false.
Why do we need to implement hash(into:) when conforming to Hashable?
Because hashing combines properties to create a unique hash value for collections like sets or dictionaries (steps 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of p1 == p2 at step 3?
Afalse
Btrue
Cerror
Dnil
💡 Hint
Check the 'Result' column in row for step 3 in execution_table.
At which step is the hash value for p2 computed?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for 'Hash p2' action in execution_table.
If p1.age was 20 instead of 30, what would be the result of p1 < p2 at step 4?
Aerror
Btrue
Cfalse
Dnil
💡 Hint
Compare ages in step 4; if p1.age < p2.age, < returns true.
Concept Snapshot
Equatable: implement static func == to compare equality.
Hashable: implement func hash(into:) to provide hash value.
Comparable: implement static func < to define order.
Use these to compare, hash, and sort your types easily.
Full Transcript
This example shows a Swift struct Person that adopts Equatable, Hashable, and Comparable protocols. It implements the required methods: == to check if two persons have the same name and age, hash(into:) to combine name and age into a hash value, and < to compare persons by age. Two instances p1 and p2 are created with different names and ages. The program compares them for equality and order, showing false for both because their names and ages differ. Hash values are computed for both. This demonstrates how these protocols let you compare, hash, and sort custom types in Swift.