Derived Behavior: Optionals and Enums

Episode #149 • Jun 14, 2021 • Subscriber-Only

We will explore two more domain transformations in the Composable Architecture. One comes with the library: the ability to embed a smaller domain, optionally, in a larger domain. Another we will build from scratch: the ability to embed smaller domains in the cases of an enum!

Optionals and Enums
Introduction
00:05
Optional state: a fact prompt domain
01:47
A fact prompt view
17:56
Enum state
25:47
Switch stores
45:27
Environment objects
56:26
Statically safer switch stores
61:54
Runtime exhaustivity
65:50
Performant switch evaluation
74:01
Next time: the point
78:27

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Introduction

And just like that we have a pretty comprehensive test that exercises multiple parts of the application and even exercises how multiple domains interact with each other. The fact that we can execute logic in the domain of a single counter amongst an entire list of counters is kind of amazing. Just imagine trying to get this kind of testability and modularity with a vanilla SwiftUI view model. It’s hard to picture, but we will actually be taking a look at that soon enough.

However, before that we want to explore a few more tools for transforming domains. We just saw that the Composable Architecture comes with a tool for transforming a reducer on a local domain into one that works on an entire collection of that domain, along with a tool for transforming a store of a collection of domain into a store that focuses on just one single element of the collection.

That’s cool, but there are a lot of other data structures we may want to pick apart in that way. For example, what about optional state? Or more generally enum state? It’s possible to build tools similar to the .forEach higher-order reducer and ForEachStore view except that they work on optionals and enums instead of collections. To explore these concepts we are going to add a feature to our little toy application.

What if we didn’t want to show a simple alert when we got the fact from the API but instead wanted to show a banner at the bottom of the screen. And to make things a little more complex, the banner will itself have behavior of its own. We’ll add a button in the banner that allows you to fetch another, and it will even manage a little loading indicator that is displayed while the API request is in flight.

Let’s dig in.

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

Exercises

  1. Enhance the SwitchStore with a Default view that can be tacked to the end of its content block and is evaluated if none of the given CaseLet cases match.

References

GitHub Discussion: CaseLetStore (for example)

Luke Redpath • Thursday Feb 18, 2021

Earlier this year, one of our viewers, Luke Redpath, started a Composable Architecture GitHub discussion around the creation of a SwitchStore-like view that inspired the design introduced in this episode.

Composable Architecture Release 0.19.0

Brandon Williams and Stephen Celis • Monday Jun 14, 2021

After publishing this episode we released 0.19.0 of the Composable Architecture, bringing SwitchStore and CaseLet views to all users of the library.

Downloads