Swift: Create your own custom operator!

Tram Ho

Swift allows you to create your own customizable operators. This is especially useful when you deal with your own data types.

In this article, I’ll show you how you can easily create your own custom operator in Swift.

Operator Types in Swift

There are five main types of operators in Swift. All operators fall into one of the following categories:

  • The Infix operator – Used between two variables, for example 1 + 2
  • Operator Prefix – Prefix a value, eg! True
  • Postfix operator – Use after one value, for example: 4! (! means factorial. Example: the factorial of 4 is 4! = 4 * 3 * 2 * 1 = 24)
  • Operator Assignment – Update the initial value by performing an operation on it. For example. num + = 1 num increases by unit.
  • Ternary operator – An operator of two symbols between three expressions. Swift has only one ternary operator called the condition? True_expression: false_expression (condition?) Operator. This is the only non-customizable operator on this list!

Any of the above operator types (except the ternary operator) are customizable. This means you can create a brand new operator for your needs.

See examples of custom operator implementations for each type of custom operator.

Custom Prefix Operator

Let’s implement a square root Emoji, which you can replace squrt (25.0) with ✔️25.0.

Since the ✔️ sign is in front of the number it is calculating, this operator is a prefix operator. With this in mind, implement a custom square root operator and test the code:

  • In the first line, you tell the program that there is a new prefix operator, ✔️.
  • Then you just create a prefix func that defines the behavior of the ✔️ operator, in this case: returns the square root of an argument.

Custom Infix Operator

Even though you already have a + operator in Swift, create a new one using emoji ➕.

As you know, a plus sign is placed between two numbers. Therefore, the type of additional operator needed is infix:

  • Again, you declare a new operator, denoted by an emoji.
  • This operator takes two arguments, lhs and rhs (left and right respectively), sums them up, and returns the result.

New Postfix operator

A great example of the postfix operator is the factorial operator (!). For example, the factorial of the year is: 5! = 5 * 4 * 3 * 2 * 1 = 120. Let’s create a custom factorial operator using emoji❗, which calculates the factorial of an integer so you can call it any integer, for example as follows: 5❗.

Use this simple factorial function to calculate the factorial:

Now, you can implement the factorial operator in the same way as in other examples, using the postfix keyword this time:

New Assignment operator

Finally, create a new assignment operator, divide one number by another, and update the original number using emoji ➗ in conjunction with Swift’s assignment operator (=).

For example:

At this time, there are no keywords for the custom assignment operator. Instead, you must declare a new infix operator ➗ = (meaning it will be placed between two numbers).

Reference source: Swift: Create Your Own Custom Operator

Share the news now

Source : Viblo