This episode is for subscribers only. To access it, and all past and future episodes, become a subscriber today!See subscription optionsorLog in
Sign up for our weekly newsletter to be notified of new episodes, and unlock access to any subscriber-only episode of your choosing!Sign up for free episode
We’ve spent the last two episodes diving deep into the world of functional setters and we’ve seen how they allow us to manipulate large data structures with precision and composition. This is only half of the picture! What about getters? Let’s explore how we access data from our structures, explore how getters compose, and see how key paths may further aid us along the way!
Find three more standard library APIs that can be used with our
The one downside to key paths being only compiler generated is that we do not get to create new ones ourselves. We only get the ones the compiler gives us.
And there are a lot of getters and setters that are not representable by key paths. For example, the
“identity” key path
KeyPath<A, A> that simply returns
self for the getter and that setting on it
leaves it unchanged. Can you think of any other interesting getters/setters that cannot be represented
by key paths?
In our Setters and Key Paths episode we showed how
kinda be seen as a “setter” by saying:
“If you tell me how to transform an
B, I will tell you how to transform an
There is also a way to think of
map as a “getter” by saying:
“If you tell me how to get a
Bout of an
A, I will tell you how to get an
[B]out of an
get with free
map function to construct getters that go even deeper into a structure.
You may want to use the data types we defined last time.
Repeat the above exercise by seeing how the free optional
map can allow you to dive deeper into an
optional value to extract out a part.
Key paths even give first class support for this operation. Do you know what it is?
Key paths aid us in getter composition for structs, but enums don’t have any stored properties. Write a
getter function for
Result that plucks out a value if it exists, such that it can compose with
Use this function with a value in
Result<User, String> to return the user’s name.
Key paths work immediately with all fields in a struct, but only work with computed properties on an enum. We saw in Algebra Data Types that structs and enums are really just two sides of a coin: neither one is more important or better than the other.
What would it look like to define an
EnumKeyPath<Root, Value> type that encapsulates the idea of
“getting” and “setting” cases in an enum?
Given a value in
EnumKeyPath<A, B> and
EnumKeyPath<B, C>, can you construct a value in
Given a value in
EnumKeyPath<A, B> and a value in
EnumKeyPath<A, C>, can you construct a value in
EnumKeyPath<A, Either<B, C>>?
A proposal has been accepted in the Swift evolution process that would allow key paths to be automatically promoted to getter functions. This would allow using key paths in much the same way you would use functions, but perhaps more succinctly: