iTranslated by AI
TCA 1.4 Migration Summary
What happened?
- The TCA 1.4 update has been released (as of November 22, 2023, 1.4.2 is the latest release)
- The main focus is support for
Macros, and a migration guide has been published- This article covers the same content as that guide
Reducer Macro
-
Simply remove the
Reducerprotocol and add@Reducer+@Reducer -struct Counter: Reducer { +struct Counter { // ... } -
While the Reducer macro by itself is almost identical to the
Reducerprotocol, it offers additional features that are only available through the macro (see below)
Introduction of CaseKeyPath
-
CasePaths can now be written using KeyPaths
Reduce { state, action in // ... } -.ifLet(\.child, action: /Action.child) { +.ifLet(\.child, action: \.child) { ChildFeature() } -
From version 1.4+, CasePath is
soft-deprecated- soft-deprecated: Something like "will be deprecated in the future"
-
This is only available for Reducers that have the Reducer macro applied
-
With the introduction of the
CasePathablemacro, runtime reflection is no longer necessary- Reflection: Inspecting one's own structure and properties
- https://github.com/pointfreeco/swift-case-paths/blob/1.0.0/Sources/CasePaths/EnumReflection.swift
Improvements to TestStore
-
CaseKeyPath eliminates the need to specify concrete enums in the
receivemethod ofTestStore.-store.receive(.child(.presented(.response(.success("Hello!")))))) +store.receive(\.child.presented.response.success) -
For
PresentationAction, an even more concise notation is available.store.receive(\.child.response.success) -
Since there is no longer a need to receive (= compare) specific enums in
receive, the requirement forAction: Equatablehas been removed.- A condition "if you use this style of action receiving exclusively" was mentioned, but its exact meaning wasn't entirely clear.
- It probably means that since Actions are no longer compared via Equatable, processing multiple
receivemethods simultaneously might lead to matching the wrong case.
- It probably means that since Actions are no longer compared via Equatable, processing multiple
- A condition "if you use this style of action receiving exclusively" was mentioned, but its exact meaning wasn't entirely clear.
-
However, for Actions containing
IdentifiedAction(described later) orStackAction, you can receive specific enums by usingsubscript(id:).store.receive(\.rows[id: 0].response.success)
Deprecation of TaskResult
- Since
Action: Equatableis no longer required (see the TestStore improvements above), the purpose ofTaskResulthas mostly vanished, so it is now soft-deprecated.- The primary role of
TaskResultwas to be a type that includes anErrorwhile still conforming toEquatable. This made it easier to makeActionconform toEquatable.
- The primary role of
- From now on, the use of Swift's
Resultis recommended.
Introduction of IdentifiedAction
- Until now, actions from UIs that exist in multiples, such as those represented in a List, were expressed as follows:
enum Action {
// ...
case row(id: State.ID, action: Action)
}
// ...
Reduce { state, action in
// ...
}
.forEach(\.rows, action: /Action.row(id:action:)) {
RowFeature()
}
- With
IdentifiedAction, this can be rewritten as follows. Since it uses CaseKeyPath, you need to use the Reducer macro.
enum Action {
// ...
case rows(IdentifiedActionOf<Nested>)
}
// ...
Reduce { state, action in
// ...
}
.forEach(\.rows, action: \.rows) {
RowFeature()
}
- If you want to access
idandactionas you did before, the difference is as follows:
-case let .row(id: id, action: .buttonTapped):
+case let .rows(.element(id: id, action: .buttonTapped)):
Reducer Macro Bugs (as of 2023/11/22)
Autocompletion bug
- This might be an issue with the order of builder type checking and macro expansion.
- For autocompletion inside the Reducer, it can be resolved by specifying the types:
var body: some Reducer<State, Action> {
- Reduce { state, action in
+ Reduce<State, Action> { state, action in
Circular Reference Error
- You cannot add extensions to code where a macro is applied within the same file.
@Reducer
struct Feature {
struct State { /* ... */ }
// ...
}
extension Feature.State { // 🛑 Circular reference error
// ...
}
- This is reported as a bug in Swift and Macros.
- The only workarounds are to move it to another file or write it directly within the structure.
Summary
- Applying Macros has changed some notations.
- There are a few bugs.
- Only available from Swift 5.9+.
- As long as you don't use them, it doesn't affect you, and you can continue with the old way. However, since it is soft-deprecated, you shouldn't leave it forever.
- Please note that there are other changes not introduced here.
Discussion