Bird
0
0

You want to overload the == operator for a struct Person to compare two persons by their id. Which implementation correctly achieves this?

hard📝 Application Q8 of 15
Swift - Operators and Expressions
You want to overload the == operator for a struct Person to compare two persons by their id. Which implementation correctly achieves this?
Afunc equals(lhs: Person, rhs: Person) -> Bool { return lhs.id == rhs.id }
Bfunc == (lhs: Person, rhs: Person) -> Bool { return lhs.id == rhs.id }
Cstatic func equals(lhs: Person, rhs: Person) -> Bool { return lhs.id == rhs.id }
Dstatic func == (lhs: Person, rhs: Person) -> Bool { return lhs.id == rhs.id }
Step-by-Step Solution
Solution:
  1. Step 1: Recall operator == overload syntax

    It must be a static function named == with two parameters returning Bool.
  2. Step 2: Check options

    Only static func == (lhs: Person, rhs: Person) -> Bool { return lhs.id == rhs.id } matches correct syntax and naming conventions.
  3. Final Answer:

    static func == (lhs: Person, rhs: Person) -> Bool { return lhs.id == rhs.id } -> Option D
  4. Quick Check:

    Equality operator overload = static func == [OK]
Quick Trick: Overload == as static func returning Bool [OK]
Common Mistakes:
  • Using instance func
  • Wrong function name
  • Not returning Bool

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes