Enumerated type

An enumerated type (English enumerated type) is a data type for variables with a finite set of values ​​. All allowable values ​​of the enumeration type are defined in the declaration of the data type constant name. It also has a sequence is defined, which determines an order of the individual values ​​. The values ​​can thus be sorted.

Enumerated types are common, for example in the programming languages ​​Pascal, Modula -2, Modula -3, Ada, Haskell, C, C and C #. In Java enumerations are supported since version 5, but here they are as real objects with object-oriented extensible means.

A distinction is untyped lists like in C, specify only the names for numeric values ​​, and type-safe enumerations as in Pascal and Java. Prevent Type-safe enumeration types that can be compared or assigned from different enumeration types. So the color BLUE from the example below, for example, would a value other than the first element of a different enumeration types (for example, fruit). A variable of type color is not assignment compatible with a value of type fruit. This has the advantage that the compiler does not allow incorrect assignments.

The simplest and most common enumeration type with exactly two valid values ​​is the logical Boolean data type:

Enumeration type boolean is { false, true }; An example of an enumerated type representing colors:

Enumeration type is color { BLUE, GREEN, RED, YELLOW }; An example of an enumerated type that represents fruits:

Enumeration type fruit is { APPLE, CHERRY, PLUM }; This allows the following assignment in which the variables wallpaper color from the Color value of the BLUE is assigned:

Variable wallpaper color is the data type color; set wallpaper color to BLUE; In a type-safe programming language the following would produce an error:

Set wallpaper color on APPLE; / / Error: APPLE is not assignment-compatible with the data type color! Web Links

  • Dean Roddey: Stupid enumeration tricks
  • Data type
87930
de