How to create a custom operator (like ~= operator) in swift ?? — 🤓🤓🤓🤓

I have seen developers using custom operators which adds meaning to the code and reduce the amount of code required to perform an operation. Creating custom operators are not encouraged. But you still can, if you wish to!!.

Types of Operators

Operators in swift fall into the following types:

  • Infix — Used between two values (ex: <value>+<value>)
  • Prefix — Used before a value (ex: !<value>)
  • Postfix — Used after a value (ex: <value>!)
  • Ternary — Two symbols inserted between three values(ex: <value1> ?<value2> :<value3>). We cannot create custom ternary operators as of now. Swift won’t allow developers to do this.

How to create Custom Operators??

Let’s create a custom operator for finding the square root of a number using the symbol.

let someVal = 25let squareRoot = someVal // result is 5

Let’s discuss the steps to create one.

  • We need to declare this symbol as a prefix operator.
prefix operator √
  • We need to create a function which accepts one parameter and perform the operation (square root).
prefix func √(lhs: Double) -> Double {return sqrt(lhs)}
Custom square root operator in swift

Let us create a custom infix operator using ◉ symbol.

The ◉ operator function will accept two values lhs and rhs and return the sum of squares of these values. ie; if lhs is 2 and rhs is 3, the result will be 4 + 9. ie; 13.

  • We need to declare this ◉ symbol as a infix operator.
infix operator ◉
  • We need to create a function which accepts two parameters and perform the operation explained above.
infix func ◉(lhs: Double, rhs: Double) -> Double {return lhs * lhs + rhs * rhs}
 func ◉(lhs: Double, rhs: Double) -> Double {return lhs * lhs + rhs * rhs}

Question: Is that it? Do we have to consider anything else while creating a custom operator??🤔🤔🤔🤔

Answer: YES 😱😱😱 !!!.. Almost forgot the operator precedence that we learned in school.

Precedence and Associativity

Apple Docs Link here.

If you declare a new operator without specifying a precedence group, it is a member of the DefaultPrecedence precedence group. DefaultPrecedence has no associativity and a precedence immediately higher than TernaryPrecedence.

Source: raywenderlich
  • Create a precedence group called SquareSumOperatorPrecedence.
precedencegroup SquareSumOperatorPrecedence {lowerThan: MultiplicationPrecedencehigherThan: AdditionPrecedenceassociativity: leftassignment: false}
  • Now replace your original declaration of the ◉ operator with the following:
infix operator ◉: SquareSumOperatorPrecedence
custom infix operator

Where to go from here?

  • Learn about operator overloading.Classes and structures can provide their own implementations of existing operators. This is known as overloading the existing operators. Ex: == , + etc..
Example from apple docs
  • Learn more about adding generic type constraints to custom operators

If you enjoyed reading this post, please share and give some clapps so others can find it 👏👏👏👏👏 !!!!

You can follow me on Medium for fresh articles. Also, connect with me on LinkedIn and Twitter.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store