paint-brush
Creating Custom Operators in Swift ☕by@aclaytonscott
2,220 reads
2,220 reads

Creating Custom Operators in Swift ☕

by Andrew ScottDecember 1st, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

When I first came across Swift code that contained custom <a href="https://hackernoon.com/tagged/operators" target="_blank">operators</a> 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 <a href="https://medium.com/@aclaytonscott/creating-and-distributing-swift-packages-132444f5dd1" target="_blank">post</a>, I’ve decided that in addition to being convenient they can also make your code more readable, even when using non-standard operators.

Company Mentioned

Mention Thumbnail
featured image - Creating Custom Operators in Swift ☕
Andrew Scott HackerNoon profile picture

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.

If you’ve done any programming you’ve used operators. You’re probably familiar with the basics, such as + , - , and=< for example. All languages define their operators differently, some like Java and Javascript rely mostly on a small set symbols used individually or in combinations, while others like python make use of english words for some of its operators like and , or , is , and not .

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.

Operator Syntax

Typically operator syntax can be broken down into three general types of operators, prefix , postfix , and infix .

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

prefix operators are placed before their operand and are unary operators.

Postfix

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.

Swift Operator Declarations

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.

Prefix

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.

Postfix

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.

Infix

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

Apple

Summary

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