Enumerated type


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 has values that are different from each other, and that can be compared and assigned, but are not generally specified by the programmer as having any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily.

Description

For example, the four suits in a deck of playing cards may be four enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type named suit. If a variable V is declared having suit as its data type, one can assign any 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 need not be semantically complete or compatible in any sense. For example, an enumerated type called color may be defined to consist of the enumerators Red, Green, Zebra, Missing, and Bacon. In some languages, the declaration of an enumerated type also intentionally defines an ordering of its members ; in others, the enumerators are unordered ; in others still, an implicit ordering arises from the compiler concretely representing enumerators as integers.
Some enumerator types may be built into the language. The Boolean type for example is often a pre-defined enumeration of the 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, 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, to have a value of red, the variable red would be declared and assigned some arbitrary value, usually an integer constant. The variable red would then be assigned to. 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. 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. For instance:
myColor = TRIANGLE
can be forbidden, whilst
myColor = RED
is accepted, even if and are both internally represented with the value.
Conceptually, an enumerated type is similar to a list of nominals, 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

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 in several programming languages

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:

var
suit: ;

The declaration will often appear in a type synonym declaration, such that it can be used for multiple variables:

type
cardsuit = ;
card = record
suit: cardsuit;
value: 1.. 13;
end;
var
hand: array of card;
trump: cardsuit;


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:

var
suitcount: array of integer;

Ada

In Ada, the definition of enumerated types is similar to Pascal, replacing the use of "=" with "is":
type Cardsuit 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:

for Cardsuit use
;

Ada also allows the number of bits of the enumeration to be specified in a straightforward manner, unlike C-style languages which have inconsistent support often leading to subtle miscompilation errors. For example:
for Cardsuit'Size use 4; -- 4 bits
Additionally, one can use enumerations as indexes for arrays, like in Pascal, but there are attributes defined for enumerations

Shuffle : constant array of Cardsuit :=
;

Like Modula-3 Ada treats Boolean and Character as special pre-defined enumerated types. Unlike Modula-3 one can also define own character types:

type Cards is ;

PL/I

Neither the original PL/I nor ANSI ANSI X3.53–1976 has enumerations, but the IBM Enterprise PL/I has ordinal types.
An ordinal variable cannot be used directly as a subscript or as a DO TO limit. However,
  1. The type functions first and last, and the builtin functions binaryvalue, ordinalpred and ordinalsucc, allow it indirectly.
  2. DO UOTHRU and DO DOWNTO support ordinals

define ordinal foo ;
declare ix ordinal foo,
bar:last) fixed bin;
do i = first to last;
bar=13;
end;
do ix=first upthru last;
i = binaryvalue;
bar = bar + 1;
end;
do ix=first repeat ordinalsucc until /* stop on wrap around. */;
i = binaryvalue;
display ;
end;

C and syntactically similar languages

C

The original K&R dialect of the programming language C had no enumerated types. In C, enumerations are created by explicit definitions which use the enum keyword and are reminiscent of struct and union definitions:

typedef enum CardSuit;
typedef struct Card;
Card hand;
CardSuit trump;

C exposes the integer representation of enumeration values directly to the programmer. Integers and enum values can be mixed freely, and all arithmetic operations on enum values are permitted. It is even possible for an enum variable to hold an integer that does not represent any of the enumeration values. In fact, according to the language definition, the above code will define Clubs, Diamonds, Hearts, and Spades as constants of type int, which will only be converted to enum CardSuit if they are stored in a variable of that type.
C also allows the programmer to choose the values of the enumeration constants explicitly, even without type. For example,

enum CardSuit ;

could be used to define a type that allows mathematical sets of suits to be represented as an enum CardSuit by bitwise logic operations.
Since C23, the underlying type of an enumeration can be specified by the programmer, with the same syntax as C++. This allows programmers to make optimize enums by choosing a less expensive underlying type.

enum CardSuit : char ;

Enums in C are not scoped or qualified, because C lacks namespacing features. Prior to the introduction of the constexpr keyword in C23, enums were often used to declare compile-time constants without use of the C preprocessor.