Improving ergonomics/correctness for State that contains enum values containing child State #2703
Replies: 1 comment 8 replies
-
| Hi @randomeizer, thanks for exploring this! It certainly is interesting, but there are a few things to think about. One thing that comes to mind is dealing with enum cases that hold onto non-TCA features. Like if you needed to present a sheet with a simple value or a vanilla SwiftUI model. The macro would need to handle that gracefully. Also, switching on And also it seems like this tool could be built outside of TCA proper. I don't think it needs access to any of the internals. That may be the best way to prove its potential for the time being. |
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.
-
Hi there,
I'm working on a TCA app with the Observation beta, working with
enumStatevalues.Here is the example for the Reducer/State from the migration guide:
...and the matching View:
I am implementing something similar, and while it's better overall than the old
SwitchStore/CaseLetimplementation, but it's still a bit of a hack.For one, we're ignoring the state data in the actual
.activity(...)state case, and two, we're using an optional scope/state internally with anif let.What would be great is if we could do this instead:
This means we would need an
enumtype that looks something like this:It's unique to the specific state
enumbeing worked with, so it would have to be either macro-generated or hand-coded. A logical place to put it would be inside the matching stateenum.It could also be publicised as part of a
protocol, sayCaseScopable:Our state definition then becomes something like:
Ok, cool. Now we need to implement our
caseScopefunction inStore:However, we also need a way to scope in and out of the type. We could add a function into the
CaseScopableenum definition:Unfortunately, it requires an
Actiontype reference in order to receive the store.Now we implement the function:
Note that we still have to do a
!'not nil' force when scoping down, but these should theoretically never benilif the wrapping case is present.Now we can call this function in our
State.caseScopefunction:And now our original sketch should compile:
I believe that the implementation for
CaseScopableconformance could be put into a macro, but haven't gotten that far.Things I don't love about it:
Actionassociatedtype/typealiasinCaseScopable(and thus in theState). Perhaps theScopeenum could go into theResolver, butStatehas no actual connection to theResolvertype by default, so it makes writingcaseScopeharder.caseScopecould be justscopeif this was added into the main library. I needed another name to avoid recursion when scoping down initially. If renamed, this would also prevent accidentally scoping anenumlike astruct.caseScope, but I don't think there's any way around it, due toenumtype-erasing.Things I do love:
Viewis now super-clear.Anyway, interested to hear people's thoughts.
Beta Was this translation helpful? Give feedback.
All reactions