3 ms·
It's true that C allows manually setting an enum to an integral value other than those explicitly specified, but no project that I've ever seen regards such an
by kibwen 8y ago
It's true that C allows manually setting an enum to an integral value other than those explicitly specified, but no project that I've ever seen regards such an action as anything other than a bug. Can you give an example of code that's using such a thing, especially for FFI?
- olliej 8y agoThe Mac and iOS design uses explicitly valued enums in all APIs (aside from anything else it makes it much harder to accidentally break ABI). JSC also uses them extensively in the lexer and parser.
- kibwen 8y agoRust can do explicit values for C-style enums; what it currently doesn't support is the ability to set such an enum to an arbitrary (non-declared) value as C does.
- v_lisivka 8y agoProtobuf specification explicitly states that protobuf compatible implementation must support values outside of the enum range, to be forward compatible with future updates to .proto files.
- bonzini 8y agoYou could define flags as an enum with values 1, 2, 4, 8, ... and OR them together.
- auscompgeek 8y agoSuch a scenario is more appropriately represented with the bitflags crate.
- bonzini 8y agoSure, he's talking about C and FFI though. You can use bitflags but you still need to convert at language boundaries.