-
Book Overview & Buying
-
Table Of Contents
Mastering Swift 6 - Seventh Edition
By :
Custom operators offer us the flexibility to introduce and implement our own operators beyond the set provided by Swift. These new operators need to be globally declared using the operator keyword and should be accompanied by either infix, prefix, or postfix keywords for proper definition. Once a custom operator is globally defined, we can integrate it into our types using operator methods, as illustrated in the preceding section.
To look deeper into this concept, let's introduce two new operators that we can integrate into the MyPoint type used in the preceding section:
Note: The • symbol can be typed by holding down the option key and pressing the number 8 on a computer running macOS.
The first thing we need to do is to declare the operators globally. We can use the following code to achieve this:
infix...