0
0
Swiftprogramming~10 mins

Operator overloading concept in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Operator overloading concept
Define operator function
Use operator with custom type
Swift calls overloaded operator
Execute custom logic
Return result
Use result in code
Operator overloading lets you define how operators like + or * work with your own types. When you use the operator, Swift runs your custom code.
Execution Sample
Swift
struct Point {
  var x: Int
  var y: Int
  static func + (lhs: Point, rhs: Point) -> Point {
    return Point(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
  }
}
Defines how to add two Point values by adding their x and y parts.
Execution Table
StepActionlhs (Point)rhs (Point)OperationResult (Point)
1Call + operator with p1 and p2Point(x: 2, y: 3)Point(x: 4, y: 5)Add x and y separatelyPoint(x: 6, y: 8)
2Return new Point from + operatorPoint(x: 2, y: 3)Point(x: 4, y: 5)Create Point(x: 6, y: 8)Point(x: 6, y: 8)
3Use result in codeN/AN/AAssign or printPoint(x: 6, y: 8)
4EndN/AN/ANo more operationsExecution stops
💡 Operator function completes and returns the new Point, ending execution.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
p1Point(x: 2, y: 3)Point(x: 2, y: 3)Point(x: 2, y: 3)Point(x: 2, y: 3)
p2Point(x: 4, y: 5)Point(x: 4, y: 5)Point(x: 4, y: 5)Point(x: 4, y: 5)
resultN/AN/APoint(x: 6, y: 8)Point(x: 6, y: 8)
Key Moments - 3 Insights
Why does the + operator work with Point even though Point is not a built-in type?
Because we defined a static function + inside Point that tells Swift how to add two Points (see execution_table step 1).
What happens if we forget to return a new Point in the + function?
The code won't compile because the operator function must return a Point, as shown in execution_table step 2.
Does the original Point values change after using +?
No, p1 and p2 stay the same; the + operator creates and returns a new Point (see variable_tracker).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of adding Point(x: 2, y: 3) and Point(x: 4, y: 5) at step 1?
APoint(x: 6, y: 8)
BPoint(x: 8, y: 15)
CPoint(x: 2, y: 3)
DPoint(x: 4, y: 5)
💡 Hint
Check the 'Result (Point)' column in execution_table row 1.
According to variable_tracker, what is the value of 'result' after step 2?
APoint(x: 2, y: 3)
BPoint(x: 6, y: 8)
CPoint(x: 4, y: 5)
DN/A
💡 Hint
Look at the 'result' row under 'After Step 2' in variable_tracker.
If we change the + function to subtract x and y instead, how would the result at step 1 change?
APoint(x: 2, y: 3)
BPoint(x: 6, y: 8)
CPoint(x: -2, y: -2)
DPoint(x: 4, y: 5)
💡 Hint
Think about subtracting lhs.x - rhs.x and lhs.y - rhs.y instead of adding.
Concept Snapshot
Operator Overloading in Swift:
- Define static func with operator symbol inside your type.
- Function takes operands and returns result.
- Allows using operators like + with custom types.
- Original values stay unchanged; new value returned.
- Enables clean, readable code for custom operations.
Full Transcript
Operator overloading lets you define how operators like plus (+) work with your own types in Swift. You write a static function named with the operator symbol inside your type, for example, Point. This function takes two Points as input and returns a new Point as the result. When you use the + operator with Points, Swift calls your function, runs your custom code to add the x and y parts, and returns a new Point. The original Points do not change. This makes your code easy to read and use with your custom types.