Skip to content

Commit

Permalink
Add a note about nested enum reducers in docs (#3050)
Browse files Browse the repository at this point in the history
* Add a note about nested enum reducers in docs

* wip
  • Loading branch information
stephencelis committed May 6, 2024
1 parent 5cb7526 commit c0a6944
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ more concise and more powerful.
* [Destination and path reducers](#Destination-and-path-reducers)
* [Navigating to non-reducer features](#Navigating-to-non-reducer-features)
* [Synthesizing protocol conformances on State and Action](#Synthesizing-protocol-conformances-on-State-and-Action)
* [Nested enum reducers](#Nested-enum-reducers)
* [Gotchas](#Gotchas)
* [Autocomplete](#Autocomplete)
* [Circular reference errors](#Circular-reference-errors)
Expand Down Expand Up @@ -181,7 +182,7 @@ conformance:

There are a number of things the ``Reducer()`` macro does for you:

#### @CasePathable and @dynamicMemberLookup enums
### @CasePathable and @dynamicMemberLookup enums

The `@Reducer` macro automatically applies the [`@CasePathable`][casepathable-docs] macro to your
`Action` enum:
Expand Down Expand Up @@ -229,7 +230,7 @@ enum of options:
The syntax `state: \.destination?.editForm` is only possible due to both `@dynamicMemberLookup` and
`@CasePathable` being applied to the `State` enum.

#### Automatic fulfillment of reducer requirements
### Automatic fulfillment of reducer requirements

The ``Reducer()`` macro will automatically fill in any ``Reducer`` protocol requirements that you
leave off. For example, something as simple as this compiles:
Expand All @@ -248,7 +249,7 @@ with their real implementations. For example, this `Feature` reducer could be in
domain using the library's navigation tools, all without having implemented any of the domain yet.
Then, once we are ready we can start implementing the real logic and behavior of the feature.

#### Destination and path reducers
### Destination and path reducers

There is a common pattern in the Composable Architecture of representing destinations a feature can
navigate to as a reducer that operates on enum state, with a case for each feature that can be
Expand Down Expand Up @@ -428,6 +429,21 @@ You can provide any combination of
``ComposableArchitecture/_SynthesizedConformance/hashable``, or
``ComposableArchitecture/_SynthesizedConformance/sendable``.

#### Nested enum reducers

There may be times when an enum reducer may want to nest another enum reducer. To do so, the parent
enum reducer must specify the child's `Body` associated value and `body` static property explicitly:

```swift
@Reducer
enum Modal { /* ... */ }

@Reducer
enum Destination {
case modal(Modal.Body = Modal.body)
}
```

### Gotchas

#### Autocomplete
Expand Down
9 changes: 4 additions & 5 deletions Sources/ComposableArchitecture/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

/// Helps implement the conformance to the ``Reducer`` protocol for a type.
///
/// See the article <doc:Reducers> for more information about the macro and
/// ``Reducer`` protocol.
/// See the article <doc:Reducers> for more information about the macro and ``Reducer`` protocol.
@attached(
member,
names:
Expand All @@ -25,9 +24,9 @@
/// An overload of ``Reducer()`` that takes a description of protocol conformances to synthesize
/// on the State and Action types
///
/// See the article <doc:Reducers> for more information about the macro and
/// ``Reducer`` protocol, in particular the section
/// <doc:Reducers#Synthesizing-protocol-conformances-on-State-and-Action>
/// See the article <doc:Reducers> for more information about the macro and ``Reducer`` protocol,
/// in particular the section
/// <doc:Reducers#Synthesizing-protocol-conformances-on-State-and-Action>.
@attached(
member,
names:
Expand Down

0 comments on commit c0a6944

Please sign in to comment.