Unlock This Episode
Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.
Introduction
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
Exercises
Find three more standard library APIs that can be used with our
get
and^
helpers.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 returnsself
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
map
could kinda be seen as a “setter” by saying:“If you tell me how to transform an
A
into aB
, I will tell you how to transform an[A]
into a[B]
.”There is also a way to think of
map
as a “getter” by saying:“If you tell me how to get a
B
out of anA
, I will tell you how to get an[B]
out of an[A]
.”Try composing
get
with freemap
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 withget
. Use this function with a value inResult<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>
andEnumKeyPath<B, C>
, can you construct a value inEnumKeyPath<A, C>
?Given a value in
EnumKeyPath<A, B>
and a value inEnumKeyPath<A, C>
, can you construct a value inEnumKeyPath<A, Either<B, C>>
?
References
SE-0249: Key Path Expressions as Functions
Stephen Celis & Greg Titus • Tuesday Mar 19, 2019A 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: users.map(\.name)
.