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 (+...