pointfreeco / swift-composable-architecture

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.
https://www.pointfree.co/collections/composable-architecture
MIT License
12.22k stars 1.42k forks source link

Nested enum reducer error: cannot be constructed because it has no accessible initializers #3047

Closed larryonoff closed 4 months ago

larryonoff commented 4 months ago

Description

The case is pretty simple. The error occurs if you have nested macro-generated enum reducer (e.g. Feature1) in another macro-generated enum reducer (.e.g. Feature2).

Looks that issue occurs since Reducer macro doesn't take into account that macro-generated enum Reducers don't have inits.

Checklist

Expected behavior

No error occurs.

Actual behavior

Compiler reports an error: 'Type' cannot be constructed because it has no accessible initializers

Steps to reproduce

See the code below.

@Reducer(state: .equatable)
enum Feature1 {
 case a
 case b
}

@Reducer(state: .equatable)
enum Feature2 {
 case feature1(Feature1)
}

The Composable Architecture version information

1.10.2

Destination operating system

No response

Xcode version information

Version 15.3 (15E204a)

Swift Compiler version information

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
stephencelis commented 4 months ago

@larryonoff This feature was added recently: https://github.com/pointfreeco/swift-composable-architecture/pull/2814

The macro has no knowledge of what the type of reducer being fed to a case is, so outside the default, you must specify the exact reducer kind and how to construct it using a default argument:

@Reducer(state: .equatable)
enum Feature2 {
  case feature1(Feature1.Body = Feature1.body)
}

Does this work for you?

larryonoff commented 4 months ago

@stephencelis thank you! it works like a charm!

Is it in the docs already?

stephencelis commented 4 months ago

@larryonoff I don't believe it's documented yet, but we'll PR some later today!