ArticleslgStudy

science

Enumerated type

Enumerated type is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Enumerated type rather than just read about it. In short: In computer programming, an enumerated type is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language.

Enumerated type — main illustration
Enumerated type — illustration

Key takeaways

  • Enumerated type belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Enumerated type to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Enumerated type from memory before moving on to harder problems.

Reference excerpt

In computer programming, an enumerated type is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. An enumerated type can be seen as a degenerate tagged union of unit type. A variable 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 defines a set of discrete, mutually exclusive constants that are not generally written to a concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily without specification from the programmer.

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.

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 constant RED would be declared and assigned some arbitrary value, usually an integer constant. The constant 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). 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: myColor = TRIANGLE can be forbidden, while myColor = RED should be accepted, even if TRIANGLE and RED are both internally represented with the value 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 tend to have their own, oftentimes multiple, programming styles and naming conventions. The variable assigned to an enumeration is usually a noun in singular form, and frequently follows either a PascalCase or uppercase convention, while lowercase and others are seen less frequently.

Syntax by language

ALGOL 60 based languages While ALGOL 60 itself has no enumeration type, several languages descended from it do. In particular, Pascal had a strong influence on the design of Ada.

Pascal In Pascal, an enumerated type can be implicitly declared by listing the values in a parenthesised list:

The declaration will often appear in a type synonym declaration, such that it can be used for multiple variables:

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, 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:

Ada In Ada, the definition of enumerated types is similar to Pascal, replacing the use of "=" with "is":

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:

… excerpt ends here. Continue reading the full article.

Illustrations

Enumerated type illustration

Worked examples

Example 1 — a first encounter with Enumerated type

Start with the simplest possible case. Write down what Enumerated type claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Enumerated type before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Enumerated type ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Enumerated type

In research
Enumerated type appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Enumerated type in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Enumerated type is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data types, Type theory, so understanding it makes those chapters shorter.
In everyday life
Look for Enumerated type outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Enumerated type” →

How to study Enumerated type in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Enumerated type means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Enumerated type out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Enumerated type in simple terms?

In computer programming, an enumerated type is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language.

Why does Enumerated type matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Enumerated type?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Enumerated type.

Tags

  • Data types
  • Type theory

Keep exploring