Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
larryonoff opened this issue May 6, 2024 · 3 comments
Closed
3 tasks done

Comments

@larryonoff
Copy link
Contributor

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

  • I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

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
@larryonoff larryonoff added the bug Something isn't working due to a bug in the library. label May 6, 2024
@stephencelis
Copy link
Member

@larryonoff This feature was added recently: #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
Copy link
Contributor Author

@stephencelis thank you! it works like a charm!

Is it in the docs already?

@stephencelis
Copy link
Member

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

@stephencelis stephencelis removed the bug Something isn't working due to a bug in the library. label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants