3 ms·
Switch is used pretty often when UI needs to look different based on certain application states (consider any 3+ state button). I was curious if I used it recen
by dvt 7y ago
Switch is used pretty often when UI needs to look different based on certain application states (consider any 3+ state button). I was curious if I used it recently, and, in fact, here it is in some code I wrote a few months ago[1]. It's also used all the time (also happens to be fairly idiomatic) when dealing with Redux, MobX, etc.
[1] https://github.com/dvx/lofi/blob/master/src/renderer/components/Lofi/Cover/index.tsx#L232 https://github.com/dvx/lofi/blob/master/src/renderer/compone...
- edflsafoiewq 7y agoBut that, like most uses of switch, is essentially uninteresting as a control-flow construct. It's just a stylistic point to use a switch here instead of an if or a match. JS's switch has no extra power other than fallthrough anyway. Here's a poor-man's coroutine (with serializable state!) where switch plays a more interesting role. void move_robot(state* s) { switch (s->resume_at) { case 0: #define SUSPEND(resume_pt) s->resume_at = resume_pt; return; case resume_pt:; #define PAUSE(resume_pt) s->pause_time = 20; while (s->pause_time) { SUSPEND(resume_pt) } for (;;) { /* three knight moves, then forward */ for (s->i = 0; s->i != 3; s->i++) { forward(s); PAUSE(1); forward(s); PAUSE(2); turn_left(s); forward(s); PAUSE(3); } forward(s); } } }