Creating Custom Operators in Swift ☕2nd December 2018
Create clear, concise, and occasionally entertaining code using custom operators.
When I first came across Swift code that contained custom operators I immediately dismissed them as just another one of those advanced language features that the average tinkerer like myself would never have a need for. However, after I ended up using them as a very basic package example in an earlier post, I’ve decided that in addition to being convenient they can also make your code more readable, even when using non-standard operators.
What are Operators?
Taken straight from Wikipedia:
(Operators are) constructs which behave generally like functions, but which differ syntactically or semantically from usual functions.
Types of Operators
Operators typically break down into several general categories:
Assignment — Assignment operators are used to assign values to variables.
Arithmetic — Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.
Comparison — Comparison operators are used to compare values. It either returns True or False according to a condition.
Logical — Logical operators are used to create define boolean logic, i.e. And , Or , and Not .
Bitwise — Bitwise operators operate on one or more bits or binary patterns.
Typically operator syntax can be broken down into three general types of operators, prefix , postfix , and infix .
Swift, and most other programming ranges generally favor infix operators, as they are the type of operator that allow you to make use of multiple operands. infix operators are also sometimes referred to as binary operators because they operate on two targets. Using this idea prefix and postfix are also referred to as unary operators. Swift, and several other languages also support ternary operators.
prefix operators are placed before their operand and are unary operators.
postfix operators are placed after their operand and are unary operators.
Creating Custom Operators
Now that I’ve defined briefly what operators are, I’ll demonstrate how to define custom operators in Swift and a few use cases.
Declaring Custom Operators
Apple describes declaring operators here, but you can refer to this pseudo-code sample for a brief look at the syntax used. New operators are declared at a global level using the operator keyword and their associated modifier.
One thing to note is that there are limitations on how you may define your operator name. While I would have loved to define a 🌮 or 💩 operator, Swift limits you to a defined range of unicode characters.
One use case for creating a custom prefix operator is if you have a common operation where a common symbol may be just as understandable and more concise than calling a function for each use. For example, finding the square root or cube root of a number.
There are numerous great uses for a postfix operator, such as the — and ++ ones I used here. But when I saw that sickle and hammer unicode was usable in Swift.. oh boy.
As I stated earlier, infix operators tend to be the most common. Here are a few fun examples I put together that leverage them.
One thing to note here is the use of precedence groups and when defining infix operators.
Operator precedence gives some operators higher priority than others; these operators are applied first.
Operator associativity defines how operators of the same precedence are grouped together — either grouped from the left, or grouped from the right. Think of it as meaning “they associate with the expression to their left,” or “they associate with the expression to their right.”
Swift’s decision to include custom operators enables you to create concise, readable code that is tailored to your needs. They can also be a lot of fun to explain to your fellow coders if someone happens upon a ☭ or ⛹ in your projects. 😉
Creating Custom Operators in Swift ☕ was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.