Computer 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.... , an enumerated type is a data type
Data type
A data type in programming languages is an attribute of a data which tells the computer something about the kind of data it is. This involves setting constraints on the datum, such as what values it can take and what operations may be performed upon it.... (usually user-defined) consisting of a set of named constants called enumerators. The act of creating an enumerated type defines an enumeration
Enumeration
In mathematics and theoretical computer science, the broadest and most abstract definition of an enumeration of a Set is an exact listing of all of its element s .... . When an identifier
Identifier
In computer science, Identifiers are Lexical Token s that name entity. The concept is analogy to that of a "name". Identifiers are used extensively in virtually all information processing systems.... such as a variable is declared
Declaration (computer science)
In programming languages, a declaration specifies the identifier, Type system, and other aspects of language elements such as Variable#Computer_programming and Subroutine.... having an enumerated type, the variable can be assigned any of the enumerators as a value. If the programming language allows an enumerated type to be given a name, the name usually is chosen to collectively describe the enumerators in the set.
For example, the four suits in a deck of playing cards (clubs, diamonds, hearts, spades) may be four enumerators belonging to an enumerated type named suits.
Computer 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.... , an enumerated type is a data type
Data type
A data type in programming languages is an attribute of a data which tells the computer something about the kind of data it is. This involves setting constraints on the datum, such as what values it can take and what operations may be performed upon it.... (usually user-defined) consisting of a set of named constants called enumerators. The act of creating an enumerated type defines an enumeration
Enumeration
In mathematics and theoretical computer science, the broadest and most abstract definition of an enumeration of a Set is an exact listing of all of its element s .... . When an identifier
Identifier
In computer science, Identifiers are Lexical Token s that name entity. The concept is analogy to that of a "name". Identifiers are used extensively in virtually all information processing systems.... such as a variable is declared
Declaration (computer science)
In programming languages, a declaration specifies the identifier, Type system, and other aspects of language elements such as Variable#Computer_programming and Subroutine.... having an enumerated type, the variable can be assigned any of the enumerators as a value. If the programming language allows an enumerated type to be given a name, the name usually is chosen to collectively describe the enumerators in the set.
For example, the four suits in a deck of playing cards (clubs, diamonds, hearts, spades) may be four enumerators belonging to an enumerated type named suits. If a variable is declared having suits as its data type, the variable can be assigned clubs, diamonds, hearts, or spades as a value.
An enumerated type need not have a complete list of enumerators, nor do the enumerators need to be ordered. However, an enumerator is usually not repeated, and, in Java, they cannot be. Furthermore, only the enumerators that are relevant may appear in the set. For example, an enumerated type called favoriteColors could only have three enumerators: red, green, and blue.
Note: In some languages, the boolean type is a pre-defined enumerated type with two enumerators (true and false). Also, enumerators sometimes appear in upper case letters to indicate they are constants.
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 numbers
Magic number (programming)
In computer programming, the term magic number has multiple meanings. It could refer to one or more of the following:* a constant used to identify a file format or protocol;... 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 hiding
Information hiding
Information hiding in computer science is the principle of hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change 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.
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-3
Modula-3
In 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 ASCII
ASCII
American Standard Code for Information Interchange , is a coding standard that can be used for interchanging information, if the information is expressed mainly by the written form of English words.... 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 size of the enumeration to be specified:
In 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 ;
struct card 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 ;
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. Perl
Perl
In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language.... or JavaScript
JavaScript
JavaScript is a scripting language widely used for client-side web development. It was the originating Programming language dialect of the ECMAScript standard.... ) do not in general provide enumerations.
C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language 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++0x is the planned new Open standard for the C++. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003.... will improve enum functionally by introducing "enum classes", which limits the scope of enumeration items to the enumeration type only.
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java .... added enumerated types whose declaration syntax is
similar to that of C
C (programming language)
C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system.... 's:
enum Cardsuit ;
...
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 class
Class (computer science)
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share.... 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 array
Bit array
A bit array is an array data structure which compactly stores individual bits . It implements a simple set data structure storing a subset of 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 ;
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' is the third-generation programming language event-driven programming and integrated integrated development environment from Microsoft for its Component Object Model programming model.... (up to version 6) and VBA
Visual Basic for Applications
Visual Basic for Applications is an implementation of Microsoft Event-driven programming programming language Visual Basic, and associated integrated development environment , which is built into most Microsoft Office applications.... 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
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data.... languages in the ML
ML programming language
ML 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., SML
Standard ML
Standard ML is a general-purpose, Module , 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 automated theorem proving.... , OCaml and Haskell
Haskell (programming language)
Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:... ), an algebraic data type
Algebraic data type
In computer programming, an algebraic data type is a datatype each of whose value s is data from other datatypes wrapped in one of the constructors of the datatype.... with only nullary constructor
Nullary constructor
In computer programming, a nullary constructor is a Constructor that takes no Parameter s.... 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 =
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 Enumtype class
Type class
In computer science, a type class is a type system construct that supports Type polymorphism#Ad-hoc polymorphism. This is achieved by adding constraints to type variables in Parametric polymorphism#Parametric polymorphism types.... which a type can derive or implement to get a mapping between the type and Int.
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 .... 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 CLOS
CLOS
The Common Lisp Object System is the facility for object-oriented programming which is part of ANSI Common Lisp. CLOS is a dynamic programming language object system which differs radically from the OOP facilities found in more static languages such as C++ or Java .... 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.
A database is a structured collection of records or data that is stored in a computer system. The structure is achieved by organizing the data according to a database model.... s support enumerated types directly. MySQL
MySQL
MySQL is a relational database management system which has more than 11 million installations. 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.