, 10 tweets, 6 min read Read on Twitter
My second talk at @iosconfsg on behalf of @worldlinexpert was all about how Swift KeyPaths can be leveraged to build some pretty effective syntax and patterns. Here's a thread that recaps some of the key ideas ⤵️
Swift 4 has introduced a new construct called KeyPaths, which allow to defer calls to the getter or setter of a property. They essentially perform the same job than a closure would, but with less $0 hanging around!
While they don't look like much by themselves, I want to show you that with a little bit of extra code, KeyPaths can be used to unlock some very nice and clean syntax!
For instance, you can define wrappers over standard methods, like map and sorted, in order to make them accept a KeyPath rather than a closure as their argument.
But you can take it even one step further, by defining a helper function (or operator) that will transform KeyPaths into getter functions. This way, every method in the standard library that expects a getter closure can now be called with a KeyPath instead 🎉
We can keep going further, and leverage KeyPaths to implement a type-safe alternative to NSPredicate!
To reach this goal, all we are required to do is to define a Predicate type and provide some overloads for the standard operators to construct, combine and evaluate them.
The great thing is that, the more operators for which you provide overloads, the more you'll introduce nice new syntax to your codebase!
If you're curious about how this can be implemented for most of Swift operators, you can take a look at this GitHub project: github.com/vincent-pradei…
That's all for this thread! There's even more content in the full slide deck, so if you've liked what you just read, do check it out: github.com/vincent-pradei…
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Vincent Pradeilles
Profile picture

Get real-time email alerts when new unrolls (>4 tweets) are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!