First Class Functions in Swift

This is often spoken about in terms of Swift. But what is it?

Image for post
Image for post
Photo by Norman Toth on Unsplash

Swift allows functions to be used like any value when used in the language. This in the vernacular says that functions are “first-class citizens”. If you want a full example of this, scroll down to the bottom of the article and have a look (there is a Gist so you could copy-paste the code right into a Playground or a single view application too!)

Terminology

function: a group of statements that together can perform some action

Passing Functions

We can pass functions around our code, treating them much like they are a variable, and move the execution of the function around the code.

This can be thought of as moving instructions around the code — like passing instructions around the code to be executed again at a later time.

The problem

Perhaps you want to make sure that when a function completes that another block of code can be run.

For synchronous code (that is when the code is run, the current thread will wait until the code is complete) the following is a valid solution:

Image for post
Image for post
Click for Gist

The completion handler

However for asynchronous code (like network code) the program may run out of order. The solution can be to run a completion handler

Image for post
Image for post
Click for Gist

Assign functions to variables

Autonomous blocks of code are called closures in Swift. These closures can be assigned to a variable which can then be passed around the code (and even to another function) an called at an appropriate time and when the closure is called the code is executed.

The whenComplete function written above can be assigned a name, since in effect it already is a variable (for clarity I’ve shown the type of the variable, although Swift’s type inference would sort this out for you).

Image for post
Image for post
Click for Gist

Which can then effectively be passed to the outputString(str:) function as defined above

Image for post
Image for post

It should be clear that the type of this example is ()->(), but for other types of completionHandlers (or closures, whatever you wish to call them!) function in exactly the same way.

A fun example — the calculator

This is something that uses enums (with associated types, sorry.), functions and optionals. Now let us get to the example which is a fun calculator (note: your definition of fun may be different to mine).

I’ve created a function called operation that will return a function of the type ((Int, Int) -> Int)?, that is a function that has two inputs of the type Int, and outputs a Int type.

The user of this public API can request a function — now the user of the API may not be able to produce the function or change it so this helps with the idea of separation of concerns. The enum ensures that the user can request the function and doesn’t need to know the internal implementation of the function at all.

The following is just one way of how to implement such a thing. If you want to copy-paste this into a Playground you can use the link to the Gist just below the code!

Image for post
Image for post
Click for Gist

What’s that? A PRACTICAL USE for first class functions? I rather think it is.

You’re welcome.

Conclusion

This article has taken us through first-class functions in Swift with a series of examples that you can write in your code. This might even be a way to abstract your code and start to think about how you can obscure your code from users of your API, and start to write code that is available to users in a way that is both simple and clear to use.

I hope this article has helped you out. Any comments, questions or improvements? Get into contact with me on Twitter and I’ll help you out.

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