Declaring unary operator functions

As previously explained, Swift 3 removed the prefix increment and postfix increment operators. However, imagine that many members of our team have experience with other programming languages that provide these operators and they want to use them to increase the value of the age property of the different Animal instances. We can declare the following unary operators to simplify their lives while coding:

  • Prefix increment (++): We will use the operator before the variable to which it is applied (for example, ++pluto)
  • Postfix increment (++): We will use the operator after the variable to which it is applied (for example, pluto++)

In this case, both the operators use exactly the same characters; therefore, we must use either the prefix or postfix keywords in each operator's function declaration.

We have to declare operator functions with the following names and specify a single Animal argument:

  • prefix ++: This is invoked when we use the prefix increment (++) operator
  • postfix ++: This is invoked when we use the postfix increment (++) operator

All the operator functions have the same declaration. For the prefix operator, Swift passes the instance specified on the right-hand side of the operator as the argument. For the postfix operator, Swift passes the instance specified on the left-hand side of the operator as the argument. Let's consider that we have two instances of Animal, or any of its subclasses, named animal1 and animal2. If we enter ++animal1 in the Playground, Swift will invoke the prefix ++ operator function with the single argument equal to animal1. If we enter animal2++ in the Playground, Swift will invoke the postfix ++ operator function with the single argument equal to animal2.

We must add the following lines to make it possible to use prefix and postfix ++ operators to increase the age of any animal. Each function uses the += operator to increase the age and assign the result of the operation to the age property. The code file for the sample is included in the swift_3_oop_chapter_04_13 folder:

    public prefix func ++ (animal: Animal) { 
      animal.age += 1 
    } 
 
    public postfix func ++ (animal: Animal) { 
      animal.age += 1 
    } 

The following lines print the age for marie--an instance of Cat--and then apply the prefix ++ operator, print the new age, apply the postfix ++ operator, and print the new age. Remember that we created operator functions that Swift invokes under the hood whenever we use these operators. The code file for the sample is included in the swift_3_oop_chapter_04_13 folder:

    marie.printAge() 
    marie++ 
    marie.printAge() 
    ++marie 
    marie.printAge() 

The following lines show the output generated by the preceding code:

    I am 4 years old. 
    I am 5 years old. 
    I am 6 years old. 
..................Content has been hidden....................

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