Reducer Protocol: Composition, Part 1

Episode #203 • Sep 5, 2022 • Subscriber-Only

We are already seeing huge benefits from the reducer protocol, but one aspect is still not ideal, and that is how we compose reducers. We will look to result builders to solve the problem, and a new feature of them introduced in Swift 5.7.

Previous episode
Reducer Protocol: Composition, Part 1
Next episode
Locked

Unlock This Episode

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

Sign in with GitHub

Introduction

The refactor to use the reducer protocol is complete, but the final code we ended up with is… well, let’s just say not ideal.

We naively converted some reducer operators to the brand new ReducerProtocol, such as combine, pullback, optional and forEach, but the nice thing about using a protocol is that we obtain a new tool to completely re-imagine reducer composition. And that’s result builders.

Result builders give us the opportunity to reimagine what the library’s core compositional operators look like. We can remove a lot of unnecessary noise when composing lots of reducers together, we can force more correctness guarantees into the API’s usage, and we can even work around some of the compiler limitations we mentioned previously, such as variadic generics.

Let’s revisit that particular limitation in order to find inspiration for why we want result builders and how we should implement them.

Reducer builders


References

Downloads

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 62 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing