iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🏃‍♀️

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 Reducer protocol and add @Reducer

    +@Reducer
    -struct Counter: Reducer {
    +struct Counter {
    	// ...
    }
    
  • While the Reducer macro by itself is almost identical to the Reducer protocol, 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 CasePathable macro, runtime reflection is no longer necessary

Improvements to TestStore

  • CaseKeyPath eliminates the need to specify concrete enums in the receive method of TestStore.

    -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 for Action: Equatable has 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 receive methods simultaneously might lead to matching the wrong case.
  • However, for Actions containing IdentifiedAction (described later) or StackAction, you can receive specific enums by using subscript(id:).

    store.receive(\.rows[id: 0].response.success)
    

Deprecation of TaskResult

  • Since Action: Equatable is no longer required (see the TestStore improvements above), the purpose of TaskResult has mostly vanished, so it is now soft-deprecated.
    • The primary role of TaskResult was to be a type that includes an Error while still conforming to Equatable. This made it easier to make Action conform to Equatable.
  • From now on, the use of Swift's Result is 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 id and action as 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

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
  // ...
}

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