6.1.2. Enumeration IO - JulTob/Ada GitHub Wiki

The program defines an enumeration type Spices, which represents different types of spices (Basil, Cayenne, Coriander, Rosemary). It uses the Enumeration_Io generic package to handle input/output operations for this enumerated type, and then it demonstrates looping through the values of the enumeration, printing them, and providing specific descriptions for each.


WITH Text_Io;  -- For standard text input/output
USE Text_Io;

PROCEDURE UsingSpices IS
   -- Define an enumeration type for different spices
   TYPE Spices IS (Basil, Cayenne, Coriander, Rosemary);
   
   -- Create an I/O package for the Spices enumeration
   PACKAGE Spice_Io IS NEW Enumeration_Io(Spices); 
   USE Spice_Io;

BEGIN
   -- Print the list of all spices
   Put_Line("My spices are:");
   
   -- Loop through all values of the Spices enumeration
   FOR I IN Spices'RANGE LOOP  -- 'RANGE returns all values of the enum
      Put(I);  -- Print each spice name
      Put("  ");  -- Add a space between the spices
   END LOOP;
   
   New_Line(2);  -- Insert two new lines for better readability

   -- Print uses for each spice
   Put_Line("Here are some uses for my spices.");
   
   FOR I IN Spices'RANGE LOOP
      Put(I); Put(" is ");
      CASE I IS
         WHEN Basil => Put_Line("great in pesto");
         WHEN Cayenne => Put_Line("hot and spicy, works well with meat");
         WHEN Coriander => Put_Line("excellent in soups");
         WHEN Rosemary => Put_Line("a subtle taste in sauces");
      END CASE;
   END LOOP;
END;

Enumeration Types:

The program defines an enumeration type Spices, which is a custom type representing a set of possible values (Basil, Cayenne, Coriander, Rosemary). Enumerations are great when you want a variable to only take on a limited set of predefined values.

TYPE Spices IS (Basil, Cayenne, Coriander, Rosemary);

Enumeration_Io Package:

The program uses a generic package, Enumeration_Io, to handle I/O for enumerated types. This package allows you to easily print and read values of enumeration types.

PACKAGE Spice_Io IS NEW Enumeration_Io(Spices);
USE Spice_Io;

Enumeration Attributes:

The loop FOR I IN Spices'RANGE LOOP demonstrates the use of the 'RANGE attribute, which returns all the possible values in the Spices enumeration. This allows us to iterate through each spice in a clean and concise way.

FOR I IN Spices'RANGE LOOP
   Put(I); Put("  ");
END LOOP;

Control Flow - CASE Statement:

The CASE statement is used to handle different values of the Spices type, allowing the program to output a specific description for each spice.

CASE I IS
   WHEN Basil => Put_Line("great in pesto");
   WHEN Cayenne => Put_Line("hot and spicy, works well with meat");
   WHEN Coriander => Put_Line("excellent in soups");
   WHEN Rosemary => Put_Line("a subtle taste in sauces");
END CASE;
  • Enumerations: It shows how to declare and use enumerated types in Ada.
  • Attributes: It introduces Ada’s 'RANGE attribute to iterate over all possible values of an enumeration.
  • Enumeration I/O: It demonstrates how to use the Enumeration_Io generic package to print enumerated values.
  • Control Structures: The CASE statement is a simple and readable way to handle multiple conditions, especially when dealing with enumerated types.