It seems to me that a lot, if not most, compilers treat enumerated types as int underneath. In C/gcc, enums are compiled to int. In C#/Visual C#, you can change the underlying data type with something like:enum Days : byte {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};but the default is int (second paragraph) if you don't change the type.For obvious reasons, ints are efficient. They fly through the ALU with ease and avoid useless string comparisons.Upon first glance, it doesn't seem that interpreted languages have enumerated types built-in (Ruby, Perl, JavaScript, others I'm sure). They can be emulated though; i.e. in JavaScript.My questions:Why do (seemingly) most compilers use int as the underlying type? Is the reason historical/performance-related?Are there languages that compile enumerated types to something other than int (or byte, long, numerical types...)? If so, why did they choose to do it differently?
Enumerated types and their interpretation by compilers
compiler;interpreters;enum
The first part of your question will be difficult to answer because that decision was made long ago and one would need to ask the creators of languages such as C to know for sure.Your second point is quite answerable.Java created the enum type in response to the widespread use of int constants which do not behave well (Effective Java, Second Edition, Bloch, Item 30/Page 147). For example, you can multiply two integer constants in a nonsensical manner but you cannot do the same with two enum values. It is possible to multiple RED with BLUE when using integer constants, but not enums. What does that mean, anyway?In addition, this decision was made because the enumeration type can be smarter. It can contain values as well as encapsulate behavior inside each enum value. Rather than having a switch over the enum values which is brittle and a maintenance problem, invert the logic and put the body of each case inside each individual enum. Now call a method on your enum variable, and use dynamic dispatch to abstract your switch. Now the enum has its values and behavior localized to the same class.C++ 11 now includes the enum class construct which was done because languages such as Java implemented intelligent enums with good success. (The C++ Programming Language, 4th Edition, Stroustrup, Section 8.4/Page 218).