First Class Functions in Swift
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!)
function: a group of statements that together can perform some action
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.
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:
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
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.
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).
Which can then effectively be passed to the
outputString(str:) function as defined above
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!
What’s that? A PRACTICAL USE for first class functions? I rather think it is.
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.