5 ms·
Not really its just a static var on a switch statement nothing really bad about it, in fact its a semi nice way to handle a simple state machine.
by nwmcsween 8y ago
Not really its just a static var on a switch statement nothing really bad about it, in fact its a semi nice way to handle a simple state machine.
- repsilat 8y agoNot tooo uncommon either. It's a "reasonable" way to write generators in C (if you're masochistic Python programmer, I guess.) Switch on the last return point at the top of the function, and make all variables that need to persist between calls `static`. (I've written it before, and I swear I didn't invent it... Maybe it's just a short skip from Duff's Device? Still, using it to pick up before the last return instead of after is new trick for me!)
- geezerjay 8y agoTo my eyes it also looked like a Duff's device which kept track of the successful state.
- Piskvorrr 8y agoYup. First thing that popped into my mind - with Duff's observation following shortly thereafter. Clever...too clever by half, IMNSHO.
- fpoling 8y agoThis was modeled on early Fortran where all local variables were static and it was trivial to implement persistent state machines.
- dimatura 8y agoYes, it's basically the idea in Duff's device. Protothreads (http://dunkels.com/adam/pt/ http://dunkels.com/adam/pt/) uses a pretty similar trick.
- repsilat 8y agoAh, that's exactly the trick I was thinking of. I think I learned it from [1], but I'm pretty sure Protothreads predates that post. Thanks for the link! 1: https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html