Swift - Operator Overloading



Operator overloading is used to redefine the behavior of existing operators for custom types.

Operator Overloading in Swift

Operator overloading is a powerful technique in Swift programming. Operator overloading allows us to change the working of the existing operators like +, -, /, *, %, etc. with the customized code.

It allows you to define custom implementations for operators.

Syntax for Overloading an Operator

To overload an operator, we have to define the behaviour of that operator using the "static func" keyword.

Following is the syntax for operator overloading −

 static func operatorSymbol(lhs: Type, rhs: Type) -> ReturnType { // Implementation } 

Consider this example, where we are overloading the addition operator (+) for a Vector type:

 struct Vector { var x: Double var y: Double } static func + (lhs: Vector, rhs: Vector) -> Vector { return Vector(x: lhs.x + rhs.x, y: lhs.y + rhs.y) } 

Types of Operator Overloading

Swift supports overloading operators in three forms:

  • Infix Operators: The operators which are placed between two operands.
  • Prefix Operators: The operators which are placed before the operands.
  • Postfix Operators: The operators which are placed after the operands.

Example of Infix Operator Overloading

Swift program to overload the + operator to add two distances represented in kilometers.

 import Foundation struct Distance { var km: Int static func + (lhs: Distance, rhs: Distance) -> Distance { return Distance(km: lhs.km + rhs.km) } } let morningRun = Distance(km: 5) let eveningRun = Distance(km: 7) let totalDistance = morningRun + eveningRun print("Total distance run: \(totalDistance.km) km") 

Output

 Total distance run: 12 km 

Example of Prefix Operator Overloading

Swift program to overload the prefix -- operator to decrease a vehicle’s fuel level.

 import Foundation prefix operator -- struct FuelTank { var liters: Int static prefix func -- (tank: inout FuelTank) { tank.liters -= 5 } } var myCar = FuelTank(liters: 50) --myCar print("Fuel left in tank: \(myCar.liters) liters") 

Output

 Fuel left in tank: 45 liters 

Example of Postfix Operator Overloading

Swift program to overload a postfix ++ operator to increase a passenger count in an elevator system.

 import Foundation postfix operator ++ struct Elevator { var passengers: Int static postfix func ++ (elevator: inout Elevator) { elevator.passengers += 1 } } var officeLift = Elevator(passengers: 4) officeLift++ print("Current passengers in elevator: \(officeLift.passengers)") 

Output

 Current passengers in elevator: 5 

Operators Overloading More Examples

Practice the following examples to understand the concepts of operators overloading in Swift:

Example 1

Swift program to overload + operator to calculate the sum of two complex numbers.

 import Foundation struct ComplexNumber { var real: Double var imag: Double // Overloading + operator to add two complex numbers static func+(left: ComplexNumber, right: ComplexNumber) -> ComplexNumber { return ComplexNumber(real: left.real + right.real, imag: left.imag + right.imag) } } let complexNumber1 = ComplexNumber(real: 2.1, imag: 2.0) let complexNumber2 = ComplexNumber(real: 6.1, imag: 9.0) // Calling + operator to add two complex numbers let sumOfComplexNumbers = complexNumber1 + complexNumber2 print(sumOfComplexNumbers) 

Output

 ComplexNumber(real: 8.2, imag: 11.0) 

Example 2

Swift program to overload custom prefix operator.

 import Foundation struct Car { var price: Double // Overloading the custom prefix operator "++" to increase the price of car static prefix func ++ (carPrice: inout Car) { carPrice.price += 500000.0 } } var currentPrice = Car(price: 2500000.0) // Calling the custom ++ operator to increase the car price ++currentPrice print("Updated car price is", currentPrice.price) 

Output

 Updated car price is 2500000.0 

Defining a Custom Operator

Swift allows you to create a custom operator where you can define new operators with custom symbols. In this example, we create a custom infix operator ** to perform exponentiation between two integers.

Example

Swift program to define and use a custom ** operator to calculate compound growth like interest or investment value.

 import Foundation // Define custom infix operator for exponentiation infix operator ** : MultiplicationPrecedence func ** (lhs: Int, rhs: Int) -> Int { return Int(pow(Double(lhs), Double(rhs))) } // Calculating future value let baseAmount = 2 let years = 5 let result = baseAmount ** years print("\(baseAmount) raised to the power \(years) is \(result)") 

Output

 2 raised to the power 5 is 32 

Limitation of Operator Overloading

The following are the limitations of operator overloading −

  • In Swift, you can overload limited operators like arithmetic and customized operators.
  • While overloading operators, you are not allowed to change the precedence or associativity of the operators.
  • Swift does not support short-circuiting behaviour for overloading logical operators.
  • Do not overuse operator overloading because it makes your code difficult to read and understand.

Difference Between Operator Function and Normal Functions

The following are the major differences between the operator functions and normal functions −

Operator Function Normal Function
They are define using "static func" keyword and a custom operator symbol. They are defined using "func" keyword and the function name.
They are used to customize the behaviours of operators. They are used to complete general purpose tasks.
They are called implicitly when using operator with custom types. They are called explicitly with the help of function name.
They can be defined in index, prefix or postfix form. They can only defined in infix form.
Advertisements