Currying Initialisers in Swift

Being chef curry IRL

Robin Malhotra
Kayako Engineering
2 min readOct 5, 2016

--

Chef Curry + Swift Curry = Awesome.

Here’s a scenario I commonly encounter while building apps here at Kayako — there’s this one object (or array of objects) I have to initialise, and it involves multiple API calls running in parallel (or serially, depending on the situation).

Now, this seems straightforward enough. Wait until you have all the data ready(stored in a global variable or something), and initialise the object. Right?

Well, yeah. But what if I want to not write

And not create global variables. What if.. I want a swiftier solution?

Well, here’s a solution

By using currying to initialize objects partially, and doing a full initialization once more information is available, it helps you (the awesome Swift 🔶 Developer), write clear and succinct code and save time in the long run.

The code

So, let’s say you have a Car Object. We’ll use a struct because “Viva la value types!”, and immutability = ❤

A Motorcycle(or bike) can be thought of as engines + tires

And our Bike Class

If you curry this using something like thoughtbot’s awesome library Curry

And we can partially initialize it like so

And then simply pass in a TirePair and voila! We have a full Bike ready to go

It’s pretty elegant, shortening your code while still retaining the type safety from swift we know and love (and curry is a single file framework, so file size isn’t a problem).

If there’s any advantages I’ve missed out, or if someone’s implemented something similar in production, I’d love to hear you in the responses below.

Oh, and a recommend would be nice too 👇

--

--

Robin Malhotra
Kayako Engineering

iOS dev and NBA fan. Ex- try! Swift speaker, now learning 🖥 dev