Ada: Enumeration

Ada: Enumeration

Understand what enumeration type is.

Enumeration declaration

Enumeration types in Ada work similarly to enumeration types in other languages like C/C++ and Java. They allow you to define a group of related named constants.

An enumeration type in Ada is defined using the keyword "type" as follows:

type DAY is (MON, TUE, WED, THU, FRI, SAT, SUN);

Here, DAY is the enumeration type name and MON, TUE, etc. are the enumeration literals.

You can then define variables of this enumeration type:

Variable1 : DAY := MON;
Variable2 : DAY := WED;

You can also define enumeration subtypes to restrict the range of literals:

subtype WEEKDAY is DAY range MON..FRI;  
Variable3 : WEEKDAY := THU;  -- Legal
Variable4 : WEEKDAY := SUN; -- Illegal, SUN is not in range

The enumeration literals are essentially integer constants assigned in the order of their declaration. So MON has an internal value of 0, TUE a value of 1 and so on.


Order of elements

Each enumeration literal is assigned an integer value in the order of its declaration. So for an enumeration like:

type DAY is (MON, TUE, WED, THU, FRI, SAT, SUN);

The integer values assigned will be:

  • MON => 0

  • TUE => 1

  • WED => 2

  • etc.

So enumerations are ordered based on the integer values assigned to the literals. This ordering allows you to:

  • Compare enumeration values using relational operators (>, <, >=, <=)

  • Perform arithmetic on enumeration values

  • Define enumeration subtypes that specify a range of literals

So while enumerations are not collections in the traditional sense, they do behave like an ordered collection since there is an inherent ordering between the enumeration literals based on their integer values.


Use-Cases

Here are some common use cases for enumeration types:

  1. Representing constants: Enumeration types are useful for representing a fixed set of constants that are related. For example, you can define an enumeration for days of the week, months of the year, boolean values (True/False), etc.

  2. Representing states: Enumerations are often used to represent different states in a state machine. For example, an enumeration for traffic light states - Red, Yellow, Green.

  3. Acting as aliases: Enumeration literals can act as more readable aliases for integer values. This makes the code more self-documenting.

  4. Reducing errors: Since enumeration literals have a fixed set of values, they reduce the chance of errors due to typos or invalid values. The compiler can catch such errors.

  5. Acting as flags: An enumeration with binary options can act as a set of flags. For example, an enumeration for file permissions - Read, Write, Execute. A file can have any combination of these permissions.

  6. Interfacing with hardware: Enumerations are often used to interface with hardware registers that use numeric codes to represent different states.

  7. Reducing memory usage: Since enumeration literals map to integer values internally, they use less memory compared to storing string literals for the same purpose.

Hope this gives you some good examples of how enumerations are commonly used in programming! Let me know if you have any other questions.


Functions & Properties

Some common methods, properties and functions related to enumerations are:

  • to_integer(): This converts an enumeration literal to its corresponding integer value. This is useful when interfacing with other systems that use numeric codes.

  • first(): Returns the first literal in the enumeration. This is useful to get a default value.

  • last(): Returns the last literal in the enumeration. This can also act as a default value.

  • succ(): Returns the literal that follows the given literal in the enumeration.

  • pred(): Returns the literal that precedes the given literal in the enumeration.

  • Enumerations also support relational operators like <, >, <=, >= which compare based on the integer values assigned to the literals.

  • Enumerations support arithmetic operators. Since the literals map to integer values internally, arithmetic can be performed on them.

  • Enumerations also have properties like:

    • count: Returns the total number of literals in the enumeration.

    • min_value: Returns the integer value of the first literal.

    • max_value: Returns the integer value of the last literal.

  • Enumerations can be iterated over using a for loop, which iterates through the literals in order.

So in summary, enumerations support various methods, properties and functions related to getting information about the enumeration type, converting between integer values, comparing literals, performing arithmetic, and iterating over the literals.


Using in Case

Ada has a case statement that can be used with enumerations.

In Ada, an enumeration type is defined using:

type DAY is (MON, TUE, WED);

And then a case statement can be used like this:

case DAY'Val(day) is
   when MON => 
      Put_Line ("Monday!");
   when TUE =>
      Put_Line ("Tuesday!");      
   when WED =>
      Put_Line ("Wednesday!");  
end case;

Here:

  • DAY'Val(day) converts the enumeration literal to an integer value

  • Each case label (MON, TUE, WED) matches a literal from the enumeration type

  • The corresponding code block is executed when there is a match

Some other points about Ada's case statement:

  • It can match both enumeration types and integer types

  • It has an "others" clause to act as a default case

  • The "when" clauses are evaluated in sequential order

  • No "break" or "fallthrough" is needed - it automatically falls through to the next case

So in summary, yes Ada does support a case statement that can be used to select different code blocks based on enumeration literals, similarly to other languages.


Disclaim: This article is created with Rix AI. I'm not sure this is everything you need to know about enumerations. I have try my best to highlight most aspects I can think of.