You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there any reason why the compiler allows an implicit conversion of numbers to const enums?
An instance of a base class cannot be implicitly converted to an instance of a derived class. I would have expected the same for const enums and numbers (all const enum values "are" numbers, but not all numbers "are" const enum values). If you do not consider numbers and const enum values equialent types, that would be even more reason to disallow implicit conversion.
The text was updated successfully, but these errors were encountered:
It's to allow patterns such as n = n + 1. We actually take the view that all numbers are enum values, but that one enum value is not assignable to another enum value. In other words, numbers and enums implicitly convert to each other, but different enums do not.
It is obviously possible to construct stricter views of enum types, but those stricter views also often force you to write type assertions. It's all about which compromises you prefer.
The following code compiles, even though variable
n
obviously is assigned value that is incompatible with its type.Is there any reason why the compiler allows an implicit conversion of numbers to const enums?
An instance of a base class cannot be implicitly converted to an instance of a derived class. I would have expected the same for const enums and numbers (all const enum values "are" numbers, but not all numbers "are" const enum values). If you do not consider numbers and const enum values equialent types, that would be even more reason to disallow implicit conversion.
The text was updated successfully, but these errors were encountered: