Taking advantage of operator overloading

Swift allows us to redefine specific operators to work in a different way, based on the classes to which we apply them. For example, we can make comparison operators, such as less than (<) and greater than (>), and return the results of comparing the age value when they are applied to instances of Dog.

Tip

The redefinition of operators to work in a specific way when applied to instances of specific classes is known as operator overloading. Swift allows us to overload operators through the usage of operator functions.

An operator that works in one way when applied to an instance of a class might work differently on instances of another class. We can also redefine the overloaded operators to work on specific subclasses. For example, we can make the comparison operators work in a different way in a superclass and its subclass.

We want to be able to compare the age of the different Animal instances using the following binary operators in Swift:

  • Less than (<)
  • Less than or equal to (<=)
  • Greater than (>)
  • Greater than or equal to (>=)

We can overload operators in Swift to achieve our goals by declaring operator functions with function names that match the operators to be overloaded and receive Animal instances as arguments. In this case, the four operators are binary operators; therefore, all the operator functions receive two input parameters of the Animal type and return a Bool value. Swift invokes these functions under the hood whenever we use the operators to compare instances of Animal. We have to declare operator functions with the following names and specify two Animal arguments:

  • <: This is invoked when we use the less than (<) operator
  • <=: This is invoked when we use the less than or equal to (<=) operator
  • >: This is invoked when we use the greater than (>) operator
  • >=: This is invoked when we use the greater than or equal to (>=) operator

All the operator functions have the same declaration. Swift passes the instance specified on the left-hand side of the operator as the first argument, usually named left, and the instance on the right-hand side of the operator as the second argument, which is usually named right. Thus, we have left and right as the arguments for the operator functions, and we must return a Bool value with the result of the application of the operator, in our case, with the result of the comparison operator applied to the age property of each instance.

Let's consider that we have two instances of Animal, or any of its subclasses, named animal1 and animal2. If we enter print(animal1 < animal2) in the Playground, Swift will invoke the < operator function with left equal to animal1 and right equal to animal2. Thus, we must return a Bool value indicating whether left.age < right.age is equivalent to animal1.age < animal2.age.

We must add the following lines to make it possible to compare the age of any animal using the previously specified comparison operators. The code file for the sample is included in the swift_3_oop_chapter_04_11 folder:

    public func < (left: Animal, right: Animal) -> Bool { 
      return left.age < right.age 
    } 
 
    public func <= (left: Animal, right: Animal) -> Bool { 
      return left.age <= right.age 
    } 
 
    public func > (left: Animal, right: Animal) -> Bool { 
      return left.age > right.age 
    } 
 
    public func >= (left: Animal, right: Animal) -> Bool { 
      return left.age >= right.age 
    } 

The following lines use the four operators that will work with the Animal class and its subclasses: greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). Remember that we created operator functions that Swift invokes under the hood whenever we use the operators. In this case, we will apply the operators on instances of TerrierDog and Cat. The operators return the results of comparing the age value of the different instances. The code file for the sample is included in the swift_3_oop_chapter_04_11 folder:

    print(pluto > marie) 
    print(pluto < marie) 
    print(pluto >= marie) 
    print(pluto <= marie) 

The following screenshot shows the four operator functions and the results of their execution in the Playground when we use the operators in instances of TerrierDog and Cat:

Taking advantage of operator overloading

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.11.89