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:

1 2 3 4 5 6 | prefix operator ✔️ prefix func ✔️(num: Double) -> Double { return sqrt(num) } print(✔️25.0) // prints 5.0 |

- 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:

1 2 3 4 5 6 | infix operator ➕ func ➕(lhs: Int, rhs: Int) -> Int { return lhs + rhs // regular + operator } print(3 ➕ 6) // prints 9 |

- 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:

1 2 3 4 5 | func factorial(_ n: Int) -> Double { return (1...n).map(Double.init).reduce(1.0, *) } factorial(5) // prints 120.0 |

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

1 2 3 4 5 6 | postfix operator ❗ postfix func ❗(num: Int) -> Double { return factorial(num) } print(5❗) // prints 120.0 |

### 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:

1 2 3 4 | var num = 14.0 num ➗= 2.0 print(num) // prints 7.0 |

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).

1 2 3 4 5 6 7 8 | infix operator ➗= func ➗=(lhs: inout Double, rhs: Double) { lhs = lhs / rhs } var num = 14.0 num ➗= 2.0 print(num) // prints 7.0 |

Reference source: Swift: Create Your Own Custom Operator