Enumeration type
An enumeration type ( English enumerated type ) is a data type for variables with a finite set of values . All permissible values of the enumeration type are used in the declaration of the data type with a unique name ( identifier defined), they are symbols . An order can also be defined, which determines an order of the individual values, according to which they can then be sorted .
In the case of enumeration types, the textual name of a symbol can sometimes be used directly, as it were as a defined character string .
Supporting languages
Enumeration types are common, for example, in the programming languages Pascal , Modula-2 , Modula-3 , Ada , Haskell , C , C ++ , C # and VB.Net . In Java enumerations are only supported from version 5, but here they can be expanded as real objects with object-oriented means.
Type safety
A distinction is made between typeless enumerations as in C, which only specify names for numeric values (generally integers), and type-safe enumerations as in Pascal and Java. Type-safe enumeration types prevent values from different enumeration types from being compared or assigned . For example, the color BLAU
from the example below would be a different value than the first element of a different enumeration type (for example, APFEL
the enumeration Obst
). A variable of type Farbe
is not assignment compatible with a value of type Obst
. This has the advantage that the compiler does not allow incorrect assignments.
Examples
The simplest and most common enumeration type with exactly two valid values is the logical data type Boolean :
Aufzählungstyp boolean ist {false, true};
An example of an enumeration type that represents colors:
Aufzählungstyp Farbe ist {BLAU, GRUEN, ROT, GELB};
An example of a bullet type that represents fruit:
Aufzählungstyp Obst ist {APFEL, KIRSCHE, PFLAUME};
This enables the following assignment, in which the variable of tapetenfarbe
the type Farbe
is BLAU
assigned the value :
Variable tapetenfarbe ist vom Datentyp Farbe; setze tapetenfarbe auf BLAU;
In a type-safe programming language, the following would generate an error:
setze tapetenfarbe auf VIOLETT; //Fehler: kein vereinbartes Symbol (in Typ Farbe) setze tapetenfarbe auf APFEL; //Fehler: APFEL ist nicht zuweisungskompatibel zum Datentyp Farbe!
In the case of non-typed lists (as in the C language) it is possible
setze tapetenfarbe auf APFEL; //kein Fehler! Weil APFEL ebenso für eine Ganzzahl steht wie z. B. GELB.
In some languages, values can be assigned an integer instead of leaving the assignment to the compiler, in order to more easily convert between values and integers or to enable a bit set (see below). If the number is not specified for a later element, the compiler continues counting from the last known number:
Aufzählungstyp Farbe ist {BLAU = 1, GRUEN, ROT = 4, GELB = 8}; // GRUEN erhält implizit den Wert 2
In addition, an associated set type ( English set ) can be declared for an enumeration type . Comparisons and set operators are then available for this.
definiere Typ 'Farben' als Mengentyp über Farbe; // Menge über dem Aufzählungstyp 'Farbe' Variable warmeFarben ist vom Datentyp Farben; nehme in warmeFarben auf (ROT, GELB); falls ROT in warmeFarben dann gib aus 'Rot ist eine warme Farbe';
If such set types are not supported as a separate language element in languages with typeless enumerations, the use of bit patterns is common. For this purpose, the values are assigned powers of two as numerical values and set operators are implemented using bitwise operators :
Variable warmeFarben ist vom Datentyp integer; setze warmeFarben auf ROT ODER GELB; // warmeFarben ist jetzt 12, denn 4 ODER 8 = 12 falls (warmeFarben UND ROT) != 0 dann // 12 UND 8 ist 8 und somit nicht 0 (in einigen Sprachen ist zur Vereinfachung solcher Ausdrücke die Zahl 0 implizit 'falsch' und jede andere ganze Zahl implizit 'wahr') gib 'ROT ist eine warme Farbe' aus;
Web links
- Dean Roddey: Stupid Enumeration Tricks