In
computer programmingComputer programming is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language. The code may be a modification of an existing source or something completely new...
, an
enumerated type (also called
enumeration or
enum) is a
data typeA data type in programming languages is a set of values and the operations on those values.-Overview:Almost all programming languages explicitly include the notion of data type, though different languages may use different terminology...
consisting of a set of named
valuesIn computer science, a value is a sequence of bits that is interpreted according to some data type. It is possible for the same sequence of bits to have different values, depending on the type used to interpret its meaning...
called
elements,
members or
enumerators of the type. The enumerator names are usually
identifierAn identifier is a unique expression in a written format either by a code, by numbers or by the combination of both to distinguish variations from one to another among a class of substances, items, or objects...
s that behave as
constantsIn computer programming, a constant is a special kind of variable whose value cannot normally be altered during program execution. Many programming languages make an explicit syntactic distinction between constant and variable symbols....
in the language. A variable that has been
declaredIn programming languages, a declaration specifies the identifier, type, and other aspects of language elements such as variables and functions. It is used to announce the existence of the element to the compiler; this is important in many languages which require variables to be declared before...
as having an enumerated type can be assigned any of the enumerators as a value.
For example, the four suits in a deck of playing cards may be four enumerators named
CLUB,
DIAMOND,
HEART,
SPADE, belonging to an enumerated type named
suits. If a variable
V is declared having
suits as its data type, one can assign any of those four values to it.
The enumerators are necessarily distinct, even though some languages may allow the same enumerator to be listed twice in the type's declaration. The enumerators need not be complete or compatible in any sense. For example, an enumerated type called
color may be defined to consist of the enumerators
RED,
GREEN,
ZEBRA, and
MISSING. In some languages, the declaration of an enumerated type also defines an
orderingIn mathematics and set theory, a total order, linear order, simple order, or ordering is a binary relation on some set X. The relation is transitive, antisymmetric, and total...
of its members.
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. Many languages allow the user to define new enumerated types.
Values and variables of an enumerated type are usually implemented as fixed-length bit strings, often in a format and size compatible with some
integerIn computer science, the term integer is used to refer to a data type which represents some finite subset of the mathematical integers. These are also known as integral data types.- Value and representation :...
type. Some languages, especially
system programming languageSystem programming languages are programming languages that are statically typed, allow arbitrarily complex data structures, compiled, and meant to operate largely independently of other programs. Prototypical system programming languages are C and Modula-2...
s, allow the user to specify the bit combination to be used for each enumerator. In
type theoryIn mathematics, logic and computer science, type theory is any of several formal systems that can serve as alternatives to naive set theory, or the study of such formalisms in general...
, enumerated types are often regarded as
tagged unionIn computer science, a tagged union, also called a variant, variant record, discriminated union, or disjoint union, is a data structure used to hold a value that could take on several different, but fixed types. Only one of the types can be in use at any one time, and a tag field explicitly...
s of
unit typeIn the area of mathematical logic, and computer science known as type theory, a unit type is a type that allows only one value . The carrier associated with a unit type can be any singleton set. There is an isomorphism between any two such sets, so it is customary to talk about the unit type and...
s. 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 variable red would be declared and assigned some arbitrary value, usually an integer constant. The variable 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 as
magic numbersIn computer programming, the term magic number has multiple meanings. It could refer to one or more of the following:* a constant numerical or text value used to identify a file format or protocol;* an unnamed or ill-documented numerical constant value;...
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, made 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 hidingInformation hiding in computer science is the principle of segregation of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed...
). 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.
Conventions
In some programming standards, enumerators are conventionally written with upper case letters to indicate they are constants.
Pascal and syntactically similar languages
In Pascal, an enumerated type can be implicitly declared by listing the values in a parenthesised list:
var
suit: (clubs, diamonds, hearts, spades);
The declaration will often appear in a type synonym declaration, such that it can be used for multiple variables:
type
cardsuit = (clubs, diamonds, hearts, spades);
card = record
suit: cardsuit;
value: 1 .. 13;
end;
var
hand: array [ 1 .. 13 ] 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-3In Computer science, Modula-3 is a programming language conceived as a successor to an upgraded version of Modula-2. While it has been influential in research circles it has not been adopted widely in industry...
, 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
ASCIIThe American Standard Code for Information Interchange is a character-encoding scheme based on the ordering of the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text...
decoding and encoding.
Pascal style languages also allow for enumeration to be used as array index
var
suitcount: array [cardsuit] of integer;
Ada
In Ada the use of "=" was replaced with "is" leaving the definition quite similar:
type Cardsuit is (clubs, diamonds, hearts, spades);
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
(clubs => 1, diamonds => 2, hearts => 4, spades => 8);
Unlike C-style languages Ada also allows the number of bits of the enumeration to be specified:
for Cardsuit'Size use 4; -- 4 bits
Even more, you can use enumerations as indexes for arrays like pascal, but there are attributes defined for enumerations
Shuffle : constant array(Cardsuit) of Cardsuit :=
(Clubs => Cardsuit'Succ(Clubs), -- see attributes of enumerations 'First, 'Last, 'Succ, 'Pred
Diamonds => Hearts, --an explicit value
Hearts => Cardsuit'Last, --first enumeration value of type Cardsuit eg clubs
Spades => Cardsuit'First --last enumeration value of type Cardsuit eg spades
);
Like
Modula-3In Computer science, Modula-3 is a programming language conceived as a successor to an upgraded version of Modula-2. While it has been influential in research circles it has not been adopted widely in industry...
Ada treats
Boolean and
Character as special pre-defined (in package "
Standard") enumerated types. Unlike Modula-3 one can also define own character types:
type Cards is ("7", "8", "9", "J", "Q", "K", "A");
C and syntactically similar languages
The original K&R dialect of the C programming language did not have enumerated types, but they were added in the ANSI standard for C, which became C89. In C, enumerations are created by explicit definitions, which use the
enum keyword and are reminiscent of struct and union definitions:
enum cardsuit {
CLUBS,
DIAMONDS,
HEARTS,
SPADES
};
struct card {
enum cardsuit suit;
short int value;
} hand[13];
enum 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 (silently) 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 {
CLUBS = 1,
DIAMONDS = 2,
HEARTS = 4,
SPADES = 8
};
could be used to define a type that allows mathematical sets of suits to be represented as an
enum cardsuit by bitwise logic operations.
Typeless languages in the syntactic tradition of C (e.g.
PerlPerl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall, a linguist working as a systems administrator for NASA, in 1987, as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone...
or
JavaScriptJavaScript is an object-oriented scripting language used to enable programmatic access to objects within both the client application and other applications. It is primarily used in the form of client-side JavaScript, implemented as an integrated component of the web browser, allowing the...
) do not, in general, provide enumerations.
C++
C++C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features...
has enumeration types that are directly inherited from C's and work mostly like these, except that an enumeration is a real type in C++, so the "enum" keyword is only used when declaring the enumeration, but the name of the enumeration properly refers to the type thereafter.
C++0xC++0x is the unofficial name of the planned new standard for the C++ programming language. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003. These predecessors are informally known as C++98 and C++03...
will improve enum functionally by introducing "enum classes", which limits the scope of enumeration items to the enumeration type only.
Java
The J2SE version 5.0 of the
Java programming languageJava is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
added enumerated types whose declaration syntax is
similar to that of
CC is a general-purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
's:
enum Cardsuit { Clubs, Diamonds, Spades, Hearts };
...
Cardsuit trump ;
The Java type system, however, treats enumerations as a type separate from integers, and intermixing of enum and integer values is not allowed. In fact, an enum type in Java is actually a special compiler-generated
classIn object-oriented programming, a class is a construct that is used as a blueprint to create objects of that class. This blueprint describes the state and behavior that the objects of the class all share. An object of a given class is called an instance of the class. The class that contains that...
rather than an arithmetic type, and enum values behave as global pre-generated instances of that class. Enum types can have instance methods and a constructor (the arguments of which can be specified separately for each enum value). All enum types implicitly extend the abstract class. An enum type cannot be instantiated directly.
Internally, each enum value contains an integer, corresponding to the order in which they are declared in the source code, starting from 0. The programmer cannot set a custom integer for an enum value. This internal integer can be obtained from an enum value using the method, and the list of enum values of an enumeration type can be obtained in order using the
values method. It is generally discouraged for programmers to convert enums to integers and vice versa. Enumerated types are
Comparable, using the internal integer; as a result, they can be sorted.
The Java standard library provides some utility data structures to use with enumerations. The class implements a Set of enum values; it is implemented as a
bit arrayA bit array is an array data structure which compactly stores individual bits . It implements a simple set data structure storing a subset of {1,2,...,n} and is effective at exploiting bit-level parallelism in hardware to perform operations quickly...
, so is very compact and efficient, and is safer to use than explicit bit field manipulations. The class implements a
Map of enum values to other things; it is implemented as an array, with the integer value of the enum value serving as the index, so is very fast.
C#
Enumerated types in the C# programming language preserve most of the "small integer" semantics of C's enums. Some arithmetic operations are not defined for enums, but an enum value can be explicitly converted
to an integer and back again, and an enum variable can have values that were not declared by the enum definition. For example, given
enum Cardsuit { Clubs, Diamonds, Spades, Hearts };
the expressions
CardSuit.Diamonds + 1 and
CardSuit.Hearts - CardSuit.Clubs are allowed directly (because it makes sense to step through the sequence of values or ask how many steps there are between two values), but
CardSuit.Hearts * CardSuit.Spades is deemed to make less sense and is only allowed if the values are first converted to integers.
Visual Basic/VBA
Enumerated datatypes in
Visual BasicVisual Basic is the third-generation event-driven programming language and integrated development environment ' from Microsoft for its COM programming model...
(up to version 6) and
VBAVisual Basic for Applications is an implementation of Microsoft's event-driven programming language Visual Basic 6, and associated integrated development environment , which is built into most Microsoft Office applications. By embedding the VBA IDE into their applications, developers can build...
are automatically assigned the "
Long" datatype and also become a datatype themselves:
Enum CardSuit
Clubs
Diamonds
Hearts
Spades
End Enum
Sub EnumExample
Dim suit As CardSuit
suit = Diamonds
MsgBox suit
End Sub
Example Code in vb.Net
Enum CardSuit
Clubs
Diamonds
Hearts
Spades
End Enum
Sub EnumExample
Dim suit As CardSuit
suit = CardSuit.Diamonds
MsgBox(suit)
End Sub
Algebraic data type in functional programming
In
functional programmingIn computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...
languages in the
MLML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM...
lineage (e.g.,
SMLStandard ML is a general-purpose, modular, functional programming language with compile-time type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of theorem provers.SML is a modern descendant of the ML...
, OCaml and
HaskellHaskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry.- History :...
), an
algebraic data typeIn computer programming, an algebraic data type is a datatype each of whose values is data from other datatypes wrapped in one of the constructors of the datatype. Any wrapped datum is an argument to the constructor...
with only
nullary constructorIn computer programming, a nullary constructor is a constructor that takes no arguments.- Object-oriented constructors :In object-oriented programming, a constructor is code that is run when an object is created...
s can be used to implement an enumerated type. For example (in the syntax of SML signatures):
datatype cardsuit = Clubs | Diamonds | Hearts | Spades
type card = { suit: cardsuit; value: int }
val hand : card list
val trump : cardsuit
In these languages the small-integer representation is completely hidden from the programmer, if indeed such a representation is employed by the implementation. However, Haskell has the
Enum type classIn computer science, a type class is a type system construct that supports ad-hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types...
which a type can derive or implement to get a mapping between the type and
Int.
Lisp
Common LispCommon Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . Developed to standardize the divergent variants of Lisp which predated it, it is not an implementation but rather a language specification...
uses the member type specifier, e.g.
(deftype cardsuit
'(member club diamond heart spade))
which states that object is of type cardsuit if it is
#'eql to club, diamond, heart or spade. The member type specifier is not valid as a
CLOSThe Common Lisp Object System is the facility for object-oriented programming which is part of ANSI Common Lisp. CLOS is a dynamic object system which differs radically from the OOP facilities found in more static languages such as C++ or Java. CLOS was inspired by earlier Lisp object systems such...
parameter specializer,
however. Instead,
(eql atom), which is the equivalent to
(member atom) may be used (that is, only one member of the set may be specified with an eql type specifier, however, it may be used as a CLOS parameter specializer.) In other words, in order to define methods to cover an enumerated type, a method must be defined for each specific element of that type.
Additionally,
(deftype finite-element-set-type (&rest elements)
`(member ,@elements))
may be used to define arbitrary enumerated types at runtime. For instance
(finite-element-set-type club diamond heart spade)
would refer to a type equivalent to the prior definition of cardsuit, as of course would simply have been using
(member club diamond heart spade)
but may be less confusing with the function
#'member for stylistic reasons.
Databases
Some
databaseA database is an integrated collection of logically related records or files consolidated into a common pool that provides data for one or more multiple uses....
s support enumerated types directly.
MySQLMySQL is a relational database management system which has more than 6 million installations. MySQL stands for "My Structured Query Language". The program runs as a server providing multi-user access to a number of databases...
provides an enumerated type
ENUM with allowable values specified as strings when a table is created. The values are stored as numeric indices with the empty string stored as 0, the first string value stored as 1, the second string value stored as 2, etc. Values can be stored and retrieved as numeric indexes or string values.
External links