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.
-
Hi there,
I'm working on a TCA app with the Observation beta, working with
enum
State
values.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
/CaseLet
implementation, 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
enum
type that looks something like this:It's unique to the specific state
enum
being 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
caseScope
function inStore
:However, we also need a way to scope in and out of the type. We could add a function into the
CaseScopable
enum definition:Unfortunately, it requires an
Action
type 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 benil
if the wrapping case is present.Now we can call this function in our
State.caseScope
function:And now our original sketch should compile:
I believe that the implementation for
CaseScopable
conformance could be put into a macro, but haven't gotten that far.Things I don't love about it:
Action
associatedtype
/typealias
inCaseScopable
(and thus in theState
). Perhaps theScope
enum could go into theResolver
, butState
has no actual connection to theResolver
type by default, so it makes writingcaseScope
harder.caseScope
could be justscope
if 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 anenum
like astruct
.caseScope
, but I don't think there's any way around it, due toenum
type-erasing.Things I do love:
View
is now super-clear.Anyway, interested to hear people's thoughts.
Beta Was this translation helpful? Give feedback.
All reactions