NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

How to use operator overloading

Swift version: 5.2

Paul Hudson    @twostraws   

Operator overloading is the practice of adding new operators and modifying existing ones to do different things. Operators are those little symbols like +, *, and /, and Swift uses them in a variety of ways depending on context – a string plus another string equals a combined string, for example, whereas an integer plus another integer equals a summed integer.

To create your own operator you need to tell Swift whether it should be prefix (before its operand; the values used with it), postfix (after its operand), or infix. The most common is infix: +, -, *, and more are all infix.

To create a new operator, try adding this to a playground:

infix operator **

That’s the exponentiation operator, designed to raise one number to the power of another. Normally we’d use the pow() function for that job, but with operator overloading we can make ** work instead.

Now you need to tell Swift what to do when it sees that operator. For example, when we write something like 2 ** 4 what does that mean?

What Swift wants is a function called **, the name of our operator, where the left-hand side is one type and the right-hand side is another type. Which type is down to us, but ** is normally used with a Double on either side, so we’re going to write a function that accepts two doubles and returns a double:

func **(lhs: Double, rhs: Double) -> Double {
    return pow(lhs, rhs)
}

As you can see, the function itself is a cinch thanks to pow() – we literally just pass on the numbers. Now this code should work in your playground:

let result = 2 ** 4

For more advanced uses, you also need to specify associativity and a precedence group, but what we have is fine to start with.

Hacking with Swift is sponsored by Instabug

SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.

Start your free trial!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Listen to the Swift over Coffee podcast

Available from iOS 8.0 – learn more in my book Pro Swift

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.0/5