Operator Overloading

Operator overloading is one of the powerful features in Swift programming language. In an essence, it is basically using of -, +, ==, /, * operators on any type you’d like. Also, you can define your own operators and use them in your projects. How cool is that?

Let’s say you have a News project and you’re working with some articles:

struct Article {    
var id = 0
var title = ""
var desc = ""

init(id: Int, title: String, desc: String) {
self.id = id
self.title = title
self.desc = desc
}
}
let articleA = Article(id: 1, title: "Title #1", desc: "Article description #1")
let articleB = Article(id: 2, title: "Title #2", desc: "Article description #2")
let articleC = Article(id: 2, title: "Title #2", desc: "Article description #3")

Now, let’s say we need to check whether two articles are the same, but, logic is that they’re the same, only if id and title are the same.

Probably, we would do it in the old fashioned way, using an if statement.

if articleB.id == articleC.id && articleB.title == articleC.title {               
print("Articles are the same")
} else {
print("Articles aren't the same")
}

The one would say that there’s nothing wrong with this code. It is working as expected. Yes, that is true. Let’s say that we add one more thing into this example — let’s say we add date property to our class and that our business logic is that if id, title and the date property is the same within two objects — then that means that they’re the same.

One thing you notice at once is that our if condition is not elegant at all. It’s huge and unsustainable. Add more to it — imagine that we have this code in couple of places. Terrible. We would definitely violate the DRY principle which stands for Don’t Repeat Yourself.

Let’s make a function:

func compare(_ articleA: Article, with articleB: Article) -> Bool 
{
return articleB.id == articleC.id && articleB.title == articleC.title && articleB.date == articleC.date
}

Yes, function would do the trick. Now, we can use this function anywhere we want to. Also, if our business logic changes again — we would change it only in one place.

The code would like like this:

if compare(articleB, with: articleC) {    
print("Articles are the same")
} else {
print("Articles aren't the same")
}

But, we already have a comparison operator in Swift, == operator. Wouldn’t it be cool that we use it instead? Sure.

func ==(left: Article, right: Article) -> Bool {    
return left.id == right.id && left.title == right.title && left.date == right.date
}

Let’s look at our final code:

if articleB == articleC {    
print("Articles are the same")
} else {
print("Articles aren't the same")
}

Now we have an elegant solution.

At some point, we all used those heavy if statements. Now that you see the power of Swift’s operator overloading,

I hope you’ve enjoyed this tutorial. Have a nice day!

Like what you read? Give Djuro Alfirevic a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.