ada Parameterized Types Variant Record Structures


Example

A discriminant of a record type may influence the structure of objects. A choice of components may exists in an object according as a discriminant had had a particular value when the object was created. To support this variation, a record type's definition includes a distinction by cases that depends on the discriminant:

type Fruit is (Banana, Orange, Pear);

type Basket (Kind : Fruit) is
   record
      case Kind is
         when Banana =>
            Bunch_Size      : Positive;
            Bunches_Per_Box : Natural;
         when Pear | Orange =>
            Fruits_Per_Box  : Natural;
      end case;
   end record;

Then to create a box for bananas,

Box : Basket (Banana);

The Box object now has two record components in addition to its discriminant, Kind, namely Bunch_Size and Bunches_Per_Box.