Unlock This Episode
Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.
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!
Subscribe to Point-Free
Access this episode, plus all past and future episodes when you become a subscriber.
Already a subscriber? Log in
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
selffor 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
mapcould 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
mapas 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
mapfunction 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
mapcan 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
Resultthat plucks out a value if it exists, such that it can compose with
get. 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<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>>?
SE-0249: Key Path Expressions as FunctionsStephen Celis & Greg Titus • Tuesday Mar 19, 2019
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: