Lably
Skip to content

Enumerated type

Named set of data type values

  • 44 min read
  • 8 chapters

Related on Lably

Through Computer programming, Data type and Value (computer science)

Description

For example, the four suits in a deck of playing cards could be represented in an enum called CardSuit, containing four members named CLUBS, DIAMONDS, SPADES, and HEARTS. If a variable v is declared having CardSuit as its data type, one must assign one 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 are arbitrary and are up to the programmer; an enum named Color could contain the enumerators RED, GREEN, ZEBRA, MISSING, and BACON. In some languages, the declaration of an enumerated type also intentionally defines the total order of its members (e.g., HIGH, MEDIUM and LOW priorities); in others, the enumerators are unordered by default (e.g., ENGLISH, FRENCH, GERMAN and SPANISH supported languages); in others still, an implicit ordering arises from how the compiler chooses to internally represent the enumerators.

Some enumerator types may be built into the language. For example, the Boolean type is often a pre-defined enumeration of the two values false and true. A unit type consisting of a single value may also be defined to represent null. Many languages allow users to define new enumerated types.

Values and variables of an enumerated type are usually implemented with some integer type as the underlying representation. Some languages, especially system programming languages, 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, enumerated types are often regarded as tagged unions of unit types. Since such types are of the form 1+1+⋯+1, they may also be written as natural numbers.

Sign in to keep reading

7 more chapters are free with a Lably account.

More in Computing and communication