Bird
0
0

What is wrong with this operator overloading code?

medium📝 Debug Q7 of 15
Swift - Operators and Expressions
What is wrong with this operator overloading code?
struct Score {
    var points: Int
    static func - (lhs: Score, rhs: Score) {
        return Score(points: lhs.points - rhs.points)
    }
}
AMissing return type in function signature
BCannot overload - operator
CParameters should be optional
DFunction should be mutating
Step-by-Step Solution
Solution:
  1. Step 1: Check function signature

    The function lacks a return type, but it returns a Score instance.
  2. Step 2: Identify correction

    Must specify return type as Score to match returned value.
  3. Final Answer:

    Missing return type in function signature -> Option A
  4. Quick Check:

    Operator function must declare return type [OK]
Quick Trick: Always specify return type when operator returns a value [OK]
Common Mistakes:
  • Omitting return type
  • Thinking - can't be overloaded
  • Using mutating unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes