Is this an idiomatic way to switch between different states of my app? I'm using cond with a keyword atom for each state.
[:main.inner.cover.flex-1 {:role "main"}
(let [state @app-state]
(cond
(= :goal-input state) [goal-input the-goal app-state]
(= :goal-timer state) [goal-timer the-goal]
(= :goal-retro state) [:h1 "Goal Retro"]))]
I would use case
since all of the keywords are static.
Regarding the usage of the ratom itself - I think it's OK. But it's a very small case, it's hard to do something "bad" here.
Ah perfect, thank you. If I use case
then I wouldn't need the let anymore to deref the atom right?
Yep. And all those =
.
Nice. Thank you!
Ah, there's one difference between cond
and case
that may be important.
With cond
, if state
is not equal to any of the keywords, the value will be nil
.
But with case
, there will be an exception.
Ah okay. But I can provide a default as well, right? Also, are keywords the best way to represent these states? Is there a better way?
Yeah, but the default needs to be explicit. Just something to keep in mind. Well, it depends. Seems fine in this case.
Thanks! I appreciate the help
Yeah coming from c#/typescript it feels weird, but I don't know of a better way at this point