In computer programming, an enumerated type is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. An enumerated type can be seen as a degenerate tagged union of unit type. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an enumerated type defines a set of discrete, mutually exclusive constants that are not generally written to a concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily without specification from the programmer.
Description For example, the four suits in a deck of playing cards could be represented in an enum called CardSuit, containing four members named CLUBS, DIAMONDS, SPADES, and HEARTS. If a variable v is declared having CardSuit as its data type, one must assign one of those four values to it. Although the enumerators are usually distinct, some languages may allow the same enumerator to be listed twice in the type's declaration. The names of enumerators are arbitrary and are up to the programmer; an enum named Color could contain the enumerators RED, GREEN, ZEBRA, MISSING, and BACON. In some languages, the declaration of an enumerated type also intentionally defines the total order of its members (e.g., HIGH, MEDIUM and LOW priorities); in others, the enumerators are unordered by default (e.g., ENGLISH, FRENCH, GERMAN and SPANISH supported languages); in others still, an implicit ordering arises from how the compiler chooses to internally represent the enumerators. Some enumerator types may be built into the language. For example, the Boolean type is often a pre-defined enumeration of the two values false and true. A unit type consisting of a single value may also be defined to represent null. Many languages allow users to define new enumerated types. Values and variables of an enumerated type are usually implemented with some integer type as the underlying representation. Some languages, especially system programming languages, allow the user to specify the bit combination to be used for each enumerator, which can be useful to efficiently represent sets of enumerators as fixed-length bit strings. In type theory, enumerated types are often regarded as tagged unions of unit types. Since such types are of the form 1+1+⋯+1, they may also be written as natural numbers.
Rationale Some early programming languages did not originally have enumerated types. If a programmer wanted a variable, for example myColor, to have a value of RED, the constant RED would be declared and assigned some arbitrary value, usually an integer constant. The constant RED would then be assigned to myColor. Other techniques assigned arbitrary values to strings containing the names of the enumerators. These arbitrary values were sometimes referred to as magic numbers since there often was no explanation as to how the numbers were obtained or whether their actual values were significant. These magic numbers could make the source code harder for others to understand and maintain. Enumerated types, on the other hand, make the code more self-documenting. Depending on the language, the compiler could automatically assign default values to the enumerators, thereby hiding unnecessary detail from the programmer. These values may not even be visible to the programmer (see information hiding). Enumerated types can also prevent a programmer from writing illogical code such as performing mathematical operations on the values of the enumerators. If the value of a variable that was assigned an enumerator were to be printed, some programming languages could also print the name of the enumerator rather than its underlying numerical value. A further advantage is that enumerated types can allow compilers to enforce semantic correctness: myColor = TRIANGLE can be forbidden, while myColor = RED should be accepted, even if TRIANGLE and RED are both internally represented with the value 1. Conceptually, an enumerated type is similar to a list of nominals (numeric codes), since each possible value of the type is assigned a distinctive natural number. A given enumerated type is thus a concrete implementation of this notion. When order is meaningful and/or used for comparison, then an enumerated type becomes an ordinal type.
Conventions Programming languages tend to have their own, oftentimes multiple, programming styles and naming conventions. The variable assigned to an enumeration is usually a noun in singular form, and frequently follows either a PascalCase or uppercase convention, while lowercase and others are seen less frequently.
Syntax by language
ALGOL 60 based languages While ALGOL 60 itself has no enumeration type, several languages descended from it do. In particular, Pascal had a strong influence on the design of Ada.
Pascal In Pascal, an enumerated type can be implicitly declared by listing the values in a parenthesised list:
The declaration will often appear in a type synonym declaration, such that it can be used for multiple variables:
The order in which the enumeration values are given matters. An enumerated type is an ordinal type, and the pred and succ functions will give the prior or next value of the enumeration, and ord can convert enumeration values to their integer representation. Standard Pascal does not offer a conversion from arithmetic types to enumerations, however. Extended Pascal offers this functionality via an extended succ function. Some other Pascal dialects allow it via type-casts. Some modern descendants of Pascal, such as Modula-3, provide a special conversion syntax using a method called VAL; Modula-3 also treats BOOLEAN and CHAR as special pre-defined enumerated types and uses ORD and VAL for standard ASCII decoding and encoding. Pascal style languages also allow enumeration to be used as array index:
Ada In Ada, the definition of enumerated types is similar to Pascal, replacing the use of "=" with "is":
In addition to Pred, Succ, Val and Pos, Ada also supports simple string conversions via Image and Value. Similar to C-style languages, Ada allows the internal representation of the enumeration to be specified:
… excerpt ends here. Continue reading the full article.

