Codegeneration tool #2543
Replies: 1 comment 3 replies
-
Well, if you are willing to model your domain imprecisely then certainly enums are never required, and in fact the vast majority of languages do not have enums. You can always swap out: enum State {
case child1(Child1.State)
case child2(Child2.State)
} …with: struct State {
var child1: Child1.State?
var child2: Child2.State?
} However, there are a lot of problems with modeling domains in this way, and so we do not recommend it. And it would be strange to force this style onto people who use your tool.
I'm not sure I 100% understand the motivation for this approach, but I can say that referring a global mutable variable like this: public static var controller: AppModuleReceiver! …may be problematic. It opens up the possibility of people outside the module completely replacing this value. And in Swift 6 it will be a compiler error for escaping closures to capture mutable variables, and so this code will not even compile someday in the future. You will see this if you turn on concurrency warnings to the max in your code.
We personally have never felt the need for code generation in our projects. In general it's quite difficult to properly integrate these kinds of tools into projects, and we think the time spent making the tool work pales in comparison to just writing out the code for a new feature. However, that is just our personal preference. We do know others maintain tools in the community, and so it's definitely helping out some people! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello dear TCA community,
I am making a code generation tool for TCA. I modelled the structure as Modules(for reducers) which has states and actions as well as submodules(which is a reference to a module). And I use this and populate a template to generate the code.
And I have a couple of questions and need some feedback from the community to cover the edge cases and check this tool can be useful at large too.
My first question is my model treats the state as a struct. But as you know we can use enums to represent the state which can be powerful. But I think the enum states also can be representable in a struct(but we lose the mutually exclusiveness of enums states) Is my assumption correct? And can you think of a case where it must be represented as an enum.
Side note: I modelled the navigation structure in submodules and solved the problem that comes with NavigationStackStore.
My second question is related to how I want to create the code. Currently I want to make it as a standalone SPM package so I don't have to deal with "generated code" and "editable code" The trade off is I had to introduce a mechanism to forward the actions to a function or somewhere where we can edit the custom business logic related to every action. So currently I create a protocol with all the actions and forward the actions to a static reference in the Module. I hope the code explains it further.
public struct AppModule: Reducer {
}
public protocol AppModuleReceiver {
func setupLaunch(state: inout AppModule.State, action: AppModule.Action) -> Effect<AppModule.Action>
}
This also prevents the use of the generated code partially which removes a lot of flexibility. My current model works for me but since I would like this tool to be open source I would love some feedback from other people if I am making the tool restrictive.
Lastly I would like to ask you is that have you ever used a code generation tool and what do you think about them in general.
Thank you in advance
Beta Was this translation helpful? Give feedback.
All reactions