4 ms·
Rust Patterns: Enums Instead of Booleans
- almostdeadguy 7y agoThis comes up quite a bit in functional languages in the ML family (which all have ADTs like rust). Bob Harper's blog had a post about this called "boolean blindness" (which seems to be locked now?), but you can find a summary of it here: https://shreevatsa.wordpress.com/2015/01/31/boolean-blindness/ https://shreevatsa.wordpress.com/2015/01/31/boolean-blindnes...
- butterisgood 7y agoHate to tell you C and C++ switch offers the same compile time safety as Rust here. Just don’t add a “default” clause.
- almostdeadguy 7y agoSwitch in C/C++ doesn't enforce mutual exclusivity though. You can forget a "break" statement and fall through multiple cases.
- olliej 7y agoI believe there are warning flags you can turn on for this, and compiler defines/tokens to allow fall through.
- olliej 7y agoThis is increasingly the standard model in large C and C++ projects, alongside the warning flags for incomplete switches and fall through you get fairly close to this. I feel the biggest problem with Enums in c/c++ is the default implicit bool and int conversions. They are the enemy.