But do you think you know all the features of KeyPaths? 🤨
Here are 5 things you (probably) didn't know about KeyPaths 🧵
Let’s start with #1️⃣: Whenever you use a method like `.map { }` or .filter { }`, you can pass it a KeyPath instead of a closure!
And the compiler will take care of automatically translating that KeyPath to a closure 👌
Moving on to #2️⃣: Did you know there’s actually more than one type of KeyPath?
The classic `KeyPath` type is actually a reference to a read-only property.
When we take a `KeyPath` to a writable property, we get a `WritableKeyPath`:
And when working with a reference type, we even get a `ReferenceWritableKeyPath`!
Most of the time, we don’t need to care about these different types, as the compiler will map a literal KeyPath to the correct type.
However, if we write our own APIs then these differences become important, because we’ll need to use the correct type depending on what we want to do with the KeyPath.
(And there are even a few additional types to deal with type erasure!)
Let’s move on to #3️⃣: We are all familiar with KeyPaths that reference properties, but did you know that a KeyPath can also reference a subscript?
Here, for instance, I’m taking a KeyPath to the property `name` of the second element in an array of `Person`.
From there we can simply invoke that KeyPath over the array just like we would do with a single value 👍
Now for #4️⃣, let’s talk about how we can use KeyPaths to write nice little DSLs, like this type-safe predicate syntax.
To make this syntax work in Swift, we actually only need to implement a single function!
We just need to implement an overload of the operator `>`, that takes as its left hand side a KeyPath and as its right hand side a constant, and then use the KeyPath to compare the value of the property to that of the constant!
Finally for #5️⃣: Did you know we can use dynamic member lookup with a KeyPath?
Consider the struct `Order` I’ve just declared: I would like to be able to access the properties of the `address` directly on the struct itself:
To do so, we start by annotating the struct with @dynamicMemberLookup.
Then, we implement a subscript that takes as its argument a KeyPath to a property of an Address and we invoke that KeyPath on the property `address`:
And that’s it, we can now directly access the properties of the `address`!
Since our implementation of the subscript relies on a KeyPath, this syntax is type-safe: meaning that if we try to call a property that doesn’t exist on an `address`, it will result in a compilation error 👌
That's all for this thread 🧵, these were the 5 things you (probably) didn’t know about KeyPaths!
Feel free to share the love and RT the original tweet 🥰
Don't miss my next thread and follow me @v_pradeilles ✅
You’ve heard that async / await is amazing with asynchronous code, but you're not really sure how it works? 🤨
Don’t worry! I’ve got you covered! 😌
In just a few tweets we’ll go over everything you need to know to understand how this powerful tool works! 🧵
Let’s start by talking about functions!
In Swift there are two kind of functions.
First, we have the functions that don’t need to wait on anything to compute their results:
Even though these functions can take a long time to compute their result, they’re able to compute it without needing to take a pause while they execute.
We call these functions **synchronous** functions:
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!
In a few hours I’m going to take-off ✈️ and head towards Singapore 🇸🇬 to attend @iosconfsg on behalf of @worldlinexpert 🤩
I plan on sharing pics 📸 and video 🎥 updates the whole time I’m away!
I’ll post everything right below this tweet ⤵️
First step of the trip: let’s ride the (very expensive 😱) shuttle, heading to the airport!
Now since I won’t realistically get any food on the plane before 11pm, I’m going to stack up energy on some good old junk food 🍔🥤