In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, an enumerated type (also called enumeration, enum, or factor in the
R programming language
R is a programming language for statistical computing and graphics supported by the R Core Team and the R Foundation for Statistical Computing. Created by statisticians Ross Ihaka and Robert Gentleman, R is used among data miners, bioinforma ...
, and a
categorical variable
In statistics, a categorical variable (also called qualitative variable) is a variable that can take on one of a limited, and usually fixed, number of possible values, assigning each individual or other unit of observation to a particular group or ...
in statistics) is a
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
consisting of a set of named
values
In ethics and social sciences, value denotes the degree of importance of something or action, with the aim of determining which actions are best to do or what way is best to live (normative ethics in ethics), or to describe the significance of dif ...
called ''elements'', ''members'', ''enumeral'', or ''enumerators'' of the type. The enumerator names are usually
identifier
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, physical countable object (or class thereof), or physical noncountable ...
s that behave as
constants in the language. An enumerated type can be seen as a degenerate
tagged union
In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. O ...
of
unit type
In the area of mathematical logic and computer science known as type theory, a unit type is a type that allows only one value (and thus can hold no information). The carrier (underlying set) associated with a unit type can be any singleton set. ...
. A
variable
Variable may refer to:
* Variable (computer science), a symbolic name associated with a value and whose associated value may be changed
* Variable (mathematics), a symbol that represents a quantity in a mathematical expression, as used in many ...
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 specified by the programmer as having any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily.
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
Order, ORDER or Orders may refer to:
* Categorization, the process in which ideas and objects are recognized, differentiated, and understood
* Heterarchy, a system of organization wherein the elements have the potential to be ranked a number of d ...
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
In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted ''true'' and ''false'') which is intended to represent the two truth values of logic and Boolean algebra. It is name ...
, for example is often a pre-defined enumeration of the values ''False'' and ''True''. Many languages allow users to define new enumerated types.
Values and variables of an enumerated type are usually implemented with some
integer
An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
type as the underlying representation. Some languages, especially
system programming language
A system programming language is a programming language used for system programming; such languages are designed for writing system software, which usually requires different development approaches when compared with application software. Edsger W ...
s, 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
In mathematics, logic, and computer science, a type theory is the formal presentation of a specific type system, and in general type theory is the academic study of type systems. Some type theories serve as alternatives to set theory as a foundat ...
, enumerated types are often regarded as
tagged union
In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. O ...
s of
unit type
In the area of mathematical logic and computer science known as type theory, a unit type is a type that allows only one value (and thus can hold no information). The carrier (underlying set) associated with a unit type can be any singleton set. ...
s. 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 ''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 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
In computer science, information hiding is the principle of segregation of the ''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 decisio ...
). 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 ''TRIANGLE'' and ''RED'' are both internally represented as ''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
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
tend to have their own, oftentimes multiple,
programming styles
Programming style, also known as code style, is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers read and understand sour ...
and
naming conventions
A naming convention is a convention (generally agreed scheme) for naming things. Conventions differ in their intents, which may include to:
* Allow useful information to be deduced from the names based on regularities. For instance, in Manhatta ...
. The variable assigned to an enumeration is usually a noun in singular form, and frequently follows either a
PascalCase
Camel case (sometimes stylized as camelCase or CamelCase, also known as camel caps or more formally as medial capitals) is the practice of writing phrases without spaces or punctuation. The format indicates the separation of words with a single ...
or
uppercase
Letter case is the distinction between the Letter (alphabet), letters that are in larger uppercase or capitals (or more formally ''majuscule'') and smaller lowercase (or more formally ''minuscule'') in the written representation of certain lang ...
convention, while
lowercase
Letter case is the distinction between the letters that are in larger uppercase or capitals (or more formally ''majuscule'') and smaller lowercase (or more formally ''minuscule'') in the written representation of certain languages. The writing ...
and others are seen less frequently.
Syntax in several programming languages
Pascal and syntactically similar languages
Pascal
In
Pascal
Pascal, Pascal's or PASCAL may refer to:
People and fictional characters
* Pascal (given name), including a list of people with the name
* Pascal (surname), including a list of people and fictional characters with the name
** Blaise Pascal, Fren ...
, 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 is a programming language conceived as a successor to an upgraded version of Modula-2 known as Modula-2+. While it has been influential in research circles (influencing the designs of languages such as Java, C#, and Python) it has not be ...
, 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 ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because of ...
decoding and encoding.
Pascal style languages also allow enumeration to be used as array index:
var
suitcount: array ardsuitof integer;
Ada
In
Ada
Ada may refer to:
Places
Africa
* Ada Foah, a town in Ghana
* Ada (Ghana parliament constituency)
* Ada, Osun, a town in Nigeria
Asia
* Ada, Urmia, a village in West Azerbaijan Province, Iran
* Ada, Karaman, a village in Karaman Province, ...
, 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
Additionally, one can use enumerations as indexes for arrays, like in 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 e.g., clubs
Spades => Cardsuit'First --last enumeration value of type Cardsuit e.g., spades
);
Like
Modula-3
Modula-3 is a programming language conceived as a successor to an upgraded version of Modula-2 known as Modula-2+. While it has been influential in research circles (influencing the designs of languages such as Java, C#, and Python) it has not be ...
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
C
The original
K&R dialect of the programming language
C had no enumerated types.
In C, enumerations are created by explicit definitions (the
enum
keyword by itself does not cause allocation of storage) which use the
enum
keyword and are reminiscent of
struct
In computer science, a record (also called a structure, struct, or compound data) is a basic data structure. Records in a database or spreadsheet are usually called "rows".
A record is a collection of '' fields'', possibly of different data typ ...
and
union
Union commonly refers to:
* Trade union, an organization of workers
* Union (set theory), in mathematics, a fundamental operation on sets
Union may also refer to:
Arts and entertainment
Music
* Union (band), an American rock group
** ''Un ...
definitions:
enum cardsuit ;
struct card hand 3
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 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.
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 may make 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.
C# also provides the C-like feature of being able to define specific integer values for enumerations. By doing this it is possible to perform binary operations on enumerations, thus treating enumeration values as sets of flags. These flags can be tested using binary operations or with the Enum type's builtin 'HasFlag' method.
The enumeration definition defines names for the selected integer values and is
syntactic sugar
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
, as it is possible to assign to an enum variable other integer values that are not in the scope of the enum definition.
C++
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
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++, giving added compile-time checking. Also (as with structs), the C++
enum
keyword is automatically combined with a , so that instead of naming the type
enum name
, simply name it
name
. This can be simulated in C using a typedef:
C++11
C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by ...
also provides a second kind of enumeration, called a ''scoped enumeration''. These are type-safe: the enumerators are not implicitly converted to an integer type. Among other things, this allows I/O streaming to be defined for the enumeration type. Another feature of scoped enumerations is that the enumerators do not leak, so usage requires prefixing with the name of the enumeration (e.g.,
Color::Red
for the first enumerator in the example below), unless a
using enum
declaration (introduced in
C++20
C++20 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++20 replaced the prior version of the C++ standard, called C++17. The standard was technically finalized by WG21 at the meeting in Prague in February 2020, a ...
) has been used to bring the enumerators into the current scope. A scoped enumeration is specified by the phrase
enum class
(or
enum struct
). For example:
enum class Color ;
The ''underlying type'' of an enumeration is an implementation-defined integral type that is large enough to hold all enumerated values; it does not have to be the smallest possible type. The underlying type can be specified directly, which allows "forward declarations" of enumerations:
enum class Color : long ; // must fit in size and memory layout the type 'long'
enum class Shapes : char; // forward declaration. If later there are values defined that don't fit in 'char' it is an error.
Go
Go uses the
iota
keyword to create enumerated constants.
type ByteSize float64
const (
_ = iota // ignore first value by assigning to blank identifier
KB ByteSize = 1 << (10 * iota)
MB
GB
)
Java
The J2SE version 5.0 of the
Java programming language
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
added enumerated types whose declaration syntax is
similar to that of
C:
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 or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used differentl ...
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 directly, but one can define
overloaded constructors that can then assign arbitrary values to self-defined members of the enum class. Defining getters allows then access to those self-defined members. The 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 utility classes to use with enumerations. The class implements a
Set
of enum values; it is implemented as a
bit array
A bit array (also known as bitmask, bit map, bit set, bit string, or bit vector) is an array data structure that compactly stores bits. It can be used to implement a simple set data structure. A bit array is effective at exploiting bit-level ...
, which makes it very compact and as efficient as explicit bit manipulation, but safer. The class implements a
Map
of enum values to object. It is implemented as an array, with the integer value of the enum value serving as the index.
Perl
Dynamically typed
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
languages in the syntactic tradition of C (e.g.,
Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
or
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
) do not, in general, provide enumerations. But in Perl programming the same result can be obtained with the shorthand
strings
String or strings may refer to:
*String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects
Arts, entertainment, and media Films
* ''Strings'' (1991 film), a Canadian anim ...
list
A ''list'' is any set of items in a row. List or lists may also refer to:
People
* List (surname)
Organizations
* List College, an undergraduate division of the Jewish Theological Seminary of America
* SC Germania List, German rugby union ...
and
hashes (possibly
slices):
my @enum = qw(Clubs Diamonds Hearts Spades);
my( %set1, %set2 );
@set1 = (); # all cleared
@set2 = (1) x @enum; # all set to 1
$set1 ... # false
$set2 ... # true
Raku
Raku (formerly known as Perl 6) supports enumerations. There are multiple ways to declare enumerations in Raku, all creating a back-end Map.
enum Cat ; # Using "quote-words"
enum Cat ('sphynx', 'siamese', 'bengal', 'shorthair', 'other'); # Using a list
enum Cat (sphynx => 0, siamese => 1, bengal => 2, shorthair => 3, other => 4); # Using Pair constructors
enum Cat (:sphynx(0), :siamese(1), :bengal(2), shorthair(3), :other(4)); # Another way of using Pairs, you can also use `:0sphynx`
PHP
Enums were added in PHP version 8.1.
enum CardSuit
Rust
Though Rust uses the
enum
keyword like C, it uses it to describe
tagged union
In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. O ...
s, which enums can be considered a degenerate form of. Rust's enums are therefore much more flexible and can contain struct and tuple variants.
enum Message
Swift
In C, enumerations assign related names to a set of integer values. In
Swift
Swift or SWIFT most commonly refers to:
* SWIFT, an international organization facilitating transactions between banks
** SWIFT code
* Swift (programming language)
* Swift (bird), a family of birds
It may also refer to:
Organizations
* SWIFT, ...
, enumerations are much more flexible and need not provide a value for each case of the enumeration. If a value (termed a ''raw'' value) is provided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type.
Alternatively, enumeration cases can specify associated values of any type to be stored along with each different case value, much as unions or variants do in other languages. One can define a common set of related cases as part of one enumeration, each of which has a different set of values of appropriate types associated with it.
In Swift, enumerations are a first-class type. They adopt many features traditionally supported only by classes, such as computed properties to provide additional information about the enumeration's current value, and instance methods to provide functionality related to the values the enumeration represents. Enumerations can also define initializers to provide an initial case value and can be extended to expand their functionality beyond their original implementation; and can conform to protocols to provide standard functionality.
enum CardSuit
Unlike C and
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTS ...
, Swift enumeration cases are not assigned a default integer value when they are created. In the CardSuit example above, clubs, diamonds, hearts, and spades do not implicitly equal 0, 1, 2 and 3. Instead, the different enumeration cases are fully-fledged values in their own right, with an explicitly-defined type of CardSuit.
Multiple cases can appear on a single line, separated by commas:
enum CardSuit
When working with enumerations that store integer or string raw values, one doesn't need to explicitly assign a raw value for each case because Swift will automatically assign the values.
For instance, when integers are used for raw values, the implicit value for each case is one more than the previous case. If the first case doesn't have a value set, its value is 0.
The enumeration below is a refinement of the earlier Planet enumeration, with integer raw values to represent each planet's order from the sun:
enum Planet: Int
In the example above, Planet.mercury has an explicit raw value of 1, Planet.venus has an implicit raw value of 2, and so on.
"Details are found in Swift documentation online here."
TypeScript
TypeScript adds an 'enum' data type to JavaScript.
enum Cardsuit ;
var c: Cardsuit = Cardsuit.Diamonds;
By default, enums number members starting at 0; this can be overridden by setting the value of the first:
enum Cardsuit ;
var c: Cardsuit = Cardsuit.Diamonds;
All the values can be set:
enum Cardsuit ;
var c: Cardsuit = Cardsuit.Diamonds;
TypeScript supports mapping the numeric value to its name. For example, this finds the name of the value 2:
enum Cardsuit ;
var suitName: string = Cardsuit
alert(suitName);
Python
A
enum
module was added to the Python standard library in version 3.4.
from enum import Enum
class Cards(Enum):
CLUBS = 1
DIAMONDS = 2
HEARTS = 3
SPADES = 4
There is also
for creating enumerations with automatically generated indices (starting with one):
Cards = Enum('Cards', 'CLUBS DIAMONDS HEARTS SPADES')
Python enumerations do not enforce semantic correctness (a meaningless comparison to an incompatible enumeration always returns ''False'' rather than raising a ''TypeError''):
>>> Color = Enum("Color", "RED GREEN BLUE")
>>> Shape = Enum("Shape", CIRCLE", "TRIANGLE", "SQUARE", "HEXAGON"
>>> def has_vertices(shape):
... return shape != Shape.CIRCLE
...
>>> has_vertices(Color.GREEN)
True
Fortran
Fortran only has enumerated types for interoperability with C; hence, the semantics is similar to C and, as in C, the enum values are just integers and no further type check is done. The C example from above can be written in Fortran as
enum, bind( C )
enumerator :: CLUBS = 1, DIAMONDS = 2, HEARTS = 4, SPADES = 8
end enum
Visual Basic/VBA
Enumerated datatypes in
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to:
* Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET
* Visual Basic (cl ...
(up to version 6) and
VBA are automatically assigned the "
Long
" datatype and also become a datatype themselves:
'Zero-based
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
Visual Basic, originally called Visual Basic .NET (VB.NET), is a multi-paradigm, object-oriented programming language, implemented on .NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Visua ...
Enum CardSuit
Clubs
Diamonds
Hearts
Spades
End Enum
Sub EnumExample()
Dim suit As CardSuit
suit = CardSuit.Diamonds
MessageBox.show(suit)
End Sub
Lisp
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fro ...
uses the member type specifier, e.g.,
(deftype cardsuit ()
'(member club diamond heart spade))
that 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
Common Lisp Object System
The Common Lisp Object System (CLOS) is the facility for object-oriented programming which is part of ANSI Common Lisp. CLOS is a powerful dynamic object system which differs radically from the OOP facilities found in more static languages such as ...
(CLOS) 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, 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.
Dart
Dart
Dart or DART may refer to:
* Dart, the equipment in the game of darts
Arts, entertainment and media
* Dart (comics), an Image Comics superhero
* Dart, a character from ''G.I. Joe''
* Dart, a ''Thomas & Friends'' railway engine character
* Dar ...
has a support for the most basic form of enums and has a syntax that is a lot similar with other languages supporting enums.
enum CardSuite
void main()
Note that the switch operator does not guarantee the completeness of the cases. This means if you omit one case, the compiler will not raise an error.
Algebraic data type in functional programming
In
functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declar ...
languages in the
ML lineage (e.g.,
Standard ML
Standard ML (SML) 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 the ...
(SML),
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
, and
Haskell
Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
), an
algebraic data type
In computer programming, especially functional programming and type theory, an algebraic data type (ADT) is a kind of composite type, i.e., a type formed by combining other types.
Two common classes of algebraic types are product types (i.e., t ...
with only
nullary constructor
In computer programming, a nullary constructor is a constructor that takes no arguments. Also known as a 0-argument constructor, no-argument constructors or default constructor.
Object-oriented constructors
In object-oriented programming, a c ...
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
Enum
type class
In 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. Such a constraint typically involves a type class T and ...
which a type can derive or implement to get a mapping between the type and
Int
.
Databases
Some
database
In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases sp ...
s support enumerated types directly.
MySQL
MySQL () is an open-source relational database management system (RDBMS). Its name is a combination of "My", the name of co-founder Michael Widenius's daughter My, and "SQL", the acronym for Structured Query Language. A relational database o ...
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.
Example:
CREATE TABLE shirts (
name VARCHAR(40),
size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
);
XML Schema
XML Schema
An XML schema is a description of a type of Extensible Markup Language, XML document, typically expressed in terms of constraints on the structure and content of documents of that type, above and beyond the basic syntactical constraints imposed ...
supports enumerated types through the enumeration facet used for constraining most primitive datatypes such as strings.
See also
*
Contrast set A contrast set is a bounded collection of items, each of which could fill the same slot in a given schema, syntactic structure, or other linguistic environment. The seven days of the week, the fifty United States, the eight Hawaiian islands, the let ...
References
External links
Enumerated types in C/C++Enumerated types in C#Enumerated types in XMLEnumerated types in Visual Basic
{{Data types
Data types
Type theory
Articles with example Ada code
Articles with example Python (programming language) code