4 ms·
> I've done them in C and Python before as well. In C I the like the table + function pointers approach when possible. Here is an example: https://stackoverflow
by jedimastert 9y ago
> I've done them in C and Python before as well. In C I the like the table + function pointers approach when possible. Here is an example: https://stackoverflow.com/questions/133214/is-there-a-typica.. https://stackoverflow.com/questions/133214/is-there-a-typica....
In that particular example, I'd just like to point out how super clever it is to put `NUM_STATES` at the end of the enum in the first line. I'm fairly comfortable in C, but I never really used it with/around people who have been using it forever, so I love seeing these little tricks.
- tzahola 9y agoIt’s a standard technique in C... Also, it produces tons of false warnings when you try to enforce exhaustive switch-cases on enum values.
- aphextron 9y ago>Also, it produces tons of false warnings when you try to enforce exhaustive switch-cases on enum values. Dispatch tables are the way to go. Nested switch statements turn into a nightmare. https://babbage.cs.qc.cuny.edu/courses/cs200/dispatch.html https://babbage.cs.qc.cuny.edu/courses/cs200/dispatch.html