In my compiler project, I have an enumeration that goes like
enum Result {
No,
Maybe,
Yes
};
I have put No explicitly at the first position, so that i can rely on the boolean evaluation to false. If my compiler is not sure about something, and has to wait for facts until runtime, its analysis functions will return Maybe. Used like
if(!typesEqual(t1, t2)) {
diagnose(types_unequal) << t1 << t2;
}
I wonder whether you or your company considers it bad style not to compare to No explicitly
if(typesEqual(t1, t2) == No) { /* ... */ }
Comparing explicitly seems wordy to me, but relying on the implicit boolean conversion somehow makes me feel guilty. Have you had that feeling before, and how have you dealt with it?
I’d feel guilty about it as well, because from reading the code above what would you expect the boolean
typesEqual()expression to return for aMaybe? Would it return true? Maybe! Would it return false? Maybe! We don’t know – that’s the entire point of the enum. That’s why it makes sense to explicitly compare toNo, even though it’s more verbose.