Understanding Operator Overloading in Swift.

Jatin Mishra
2 min readNov 22, 2022

Operator overloading is a way to change the behavior of existing operators like (+, -, *, etc.) or create new operators that perform special operations like in Python ** is an exponential operator.

2 ** 3 = 8 # 2 to the power of 3 equals 8

Operator Overloading can be done in two ways:

1. Changing the behavior of existing types.

Here we can take an example of + (add operator) in swift. This simply adds two operands and returns the sum.

let a = 2
let b = 8
print(a + b) // this prints 10

One fine day you want that + should multiply the operands instead of adding them. We can do this using the following code snippet.

extension Int {
static func +(lhs: Int, rhs: Int) -> Int {
return lhs * rhs
}
}

Now if you try to use the + operator:

let a = 2
let b = 8
print(a + b) // now this will print 16 i.e 2 * 8

2. Creating a new operator.

In Python, we can do something like this.

print("text" * 2) // this print "texttext"

Unfortunately Swift does not support this. Let’s add this feature in Swift.

extension String {
static func * (lhs: String, rhs: Int) -> String {
var result = ""

for _ in 0..<rhs {
result.append(lhs)
}

return result
}
}

The above code snippet will add a * operator in the String struct and return the string repeated n times.
Example:

print("text" * 2) // this will now print "texttext"

Using operators in custom types.

Imagine we have a struct called Payment.

struct Payment {
var id: Int
var value: Double
}

We want to have a way to add the payment value of multiple Payment objects to get the total payment like:

var payment1 = Payment(id: 1, value: 20)
var payment2 = Payment(id: 2, value: 50)

let totalPayment = payment1.value + payment2.value

print(totalPayment) // prints 70 i.e. 20 + 50

Thinking about the type’s behavior if we can use the + operator with two or more instances of Payment type the only logical result should be the sum of their respective “value” properties.
So we can use the power of operator overloading here.

extension Payment {
static func + (lhs: Payment, rhs: Payment) -> Double {
return lhs.value + rhs.value
}
}

Now if we again try to add “Payment” we can do

var payment1 = Payment(id: 1, value: 20)
var payment2 = Payment(id: 2, value: 50)

print(payment1 + payment2) // print 70 i.e. 20 + 50

This solution is much more graceful and clean.

Conclusion

Operator overloading is a very powerful technique to make your code cleaner and more extensible.
I hope you can get some ideas to use in your code from the above examples.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Jatin Mishra
Jatin Mishra

Written by Jatin Mishra

Senior iOS Engineer with 6+ years of experience, specializing in high-performance app development and architecture optimization. Join me for special insights.

No responses yet

Write a response