4 ms·
Do not do this. There's no reason to not use an enum here -- requiring a cast is not a bad thing. There are very few cases where you should have implicit conve
by Locke1689 12y ago
Do not do this. There's no reason to not use an enum here -- requiring a cast is not a bad thing.
There are very few cases where you should have implicit conversions. Implicit conversions basically imply that the two types are so similar that no possible semantic difference can be observed after the conversion. For example, int is implicitly convertible to long because there can be no possible information loss and almost every case where long is allowed, an int would also be allowed in that place. Even overload resolution works correctly because the overload is first tried without the implicit conversion.
Enums (or other structs), on the other hand, can easily have different semantics. For example, if the enum values are auto-assigned and you add a value into the middle of the enum, all subsequent values are now offset-increased by 1. If you then switch on the int values, but not the enum values, your semantics will change.
Here an explicit cast is a good thing -- it's saying to the compiler that I'm doing something potentially dangerous, but just trust me on this one.