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.
A unit type consisting of a single value may also be defined to represent null.
These magic numbers could make the source code harder for others to understand and maintain.
Depending on the language, the compiler could automatically assign default values to the enumerators thereby hiding unnecessary detail from the programmer.
A further advantage is that enumerated types can allow compilers to enforce semantic correctness.
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: In Ada, the use of "=" was replaced with "is" leaving the definition quite similar: In addition to Pred, Succ, Val and Pos Ada also supports simple string conversions via Image and Value.
[1] 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 and union definitions: C exposes the integer representation of enumeration values directly to the programmer.
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, 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# also provides the C-like feature of being able to define specific integer values for enumerations.
These flags can be tested using binary operations or with the enum type's builtin 'HasFlag' method.
[3] The enumeration definition defines names for the selected integer values and is syntactic sugar, as it is possible to assign to an enum variable other integer values that are not in the scope of the enum definition.
[8] Internally, each enum value contains an integer, corresponding to the order in which they are declared in the source code, starting from 0.
It is generally discouraged for programmers to convert enums to integers and vice versa.
Dynamically typed languages in the syntactic tradition of C (e.g., Perl or JavaScript) do not, in general, provide enumerations.
There are multiple ways to declare enumerations in Raku, all creating a back-end Map.
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.
Unlike C and Objective-C, Swift enumeration cases are not assigned a default integer value when they are created.
For the CardSuit example, suits can be numbered starting from 1 by writing: TypeScript adds an 'enum' data type to JavaScript.
The member type specifier is not valid as a Common Lisp Object System (CLOS) parameter specializer, however.
For instance would refer to a type equivalent to the prior definition of cardsuit, as of course would simply have been using but may be less confusing with the function #'member for stylistic reasons.
In functional programming languages in the ML lineage (e.g., Standard ML (SML), OCaml, and Haskell), an algebraic data type with only nullary constructors can be used to implement an enumerated type.
MySQL provides an enumerated type ENUM with allowable values specified as strings when a table is created.