Getters and Key Paths

Episode #8 • Mar 19, 2018 • Subscriber-Only

Key paths aren’t just for setting. They also assist in getting values inside nested structures in a composable way. This can be powerful, allowing us to make the Swift standard library more expressive with no boilerplate.

Previous episode
Getters and Key Paths
Key paths as getters
Operator overload?
What’s the point?

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!

This episode is for subscribers only.

Subscribe to Point-Free

Access this episode, plus all past and future episodes when you become a subscriber.

See plans and pricing

Already a subscriber? Log in


  1. Find three more standard library APIs that can be used with our get and ^ helpers.

  2. 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?

  3. 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 a B, 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 an A, I will tell you how to get an [B] out of an [A].”

    Try composing 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.

  4. 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?

  5. 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 get. Use this function with a value in Result<User, String> to return the user’s name.

  6. 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?

  7. Given a value in EnumKeyPath<A, B> and EnumKeyPath<B, C>, can you construct a value in EnumKeyPath<A, C>?

  8. 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 Functions

Stephen 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:\.name).