[ Table of Contents ]
An enumeration type declaration specifies a list of distinct items, enclosed in parentheses.
Enumeration Type Declaration |
type Subtype_Name is (Id1, Id2, Id3 ... ); |
where Id1, Id2, etc. are identifiers or characters literals. In either case the legal values of the type are referred to as "enumeration literals." Each of these values has a "position number" corresponding to its position in the list such that Id1 has position 0, Id2 has position 1, and the Nth value has position N-1.
Attributes of Enumeration Types
An enumeration type, T, has the following attributes: T'First, T'Last, T'Range, T'Pred, T'Succ, T'Min, T'Max, T'Image, T'Wide_Image, T'Value, T'Wide_Value, T'Pos, and T'Val (pronounced "T tick first", "T tick last", etc.). Most of these are illustrated in the example program given below, and most of them produce what you would intuitively expect based on their names.
T'Image and T'Value form a complementary pair of attributes. The former takes a value in T and returns a String representation of that value. The latter takes a String that is a representation of a value in T and returns that value.
T'Pos and T'Val form another complementary pair. The former takes a value in T and returns its position number. The latter takes a position number and returns the corresponding value of type T. Note that Appendix A contains a table listing all of the language-defined attributes. (See Related Topics, at the bottom of this page.)
Example Program Illustrating Enumeration Types and Attributes
The package Sports_Teams, which has no body, exports two enumeration types
called Baseball_Position and Football_Position. The test procedure exercises various attributes such as First, Last, Pred, Succ, Pos, Val, Image and Range, and displays the results using the Put_Line and Put procedures provided by Ada.Text_IO. |
Source Code Listing
------------------------- Sports_Teams -------------------------- -- This package exports two enumeration types. No body is needed. ------------------------------------------------------------------ package Sports_Teams is type Baseball_Position is -- Pos 0 thru 8 (Pitcher, Catcher, First_Base, Second_Base, Third_Base, Short_Stop, Right_Field, Center_Field, Left_Field); type Football_Position is -- Pos 0 thru 10 (Left_End, Left_Tackle, Left_Guard, Center, Right_Guard, Right_Tackle, Right_End, Quarterback, Left_Halfback, Right_Halfback, Fullback); end Sports_Teams; ------------------------------------------------------------------ -------------------------- Test_Teams ---------------------------- -- This test procedure illustrates the use of various attributes -- of the types, Baseball_Position and Football_Position. ------------------------------------------------------------------ with Ada.Text_IO, Sports_Teams; use Ada.Text_IO, Sports_Teams; procedure Test_Teams is BP : Baseball_Position; FP : Football_Position; PN : Integer; -- position number begin BP := Baseball_Position'First; -- First Put_Line(Baseball_Position'Image(BP) & -- Image " is the first baseball position"); FP := Football_Position'Last; -- Last Put_Line(Football_Position'Image(FP) & -- Image " is the last football position"); BP := Baseball_Position'Pred(Second_Base); -- Pred Put_Line(Baseball_Position'Image(BP) & -- Image " precedes Second_Base"); FP := Football_Position'Succ(Right_Guard); -- Succ Put_Line(Football_Position'Image(FP) & -- Image " follows Right_Guard"); FP := Football_Position'Succ(FP); -- Succ Put_Line(Football_Position'Image(FP) & -- Image " follows prior value"); PN := Baseball_Position'Pos(Short_Stop); -- Pos Put_Line(Integer'Image(PN) & -- Image " is the position # of Short_Stop"); FP := Football_Position'Val(7); -- Val Put_Line(Football_Position'Image(FP) & -- Image " is at the 7th position"); New_Line; for P in Baseball_Position'Range loop -- Range Put(Baseball_Position'Image(P) & " "); -- Image if P = Short_Stop then New_Line; end if; end loop; New_Line; New_Line; for P in Football_Position loop -- Note (1) Put(Football_Position'Image(P) & " "); -- Image if P = Right_Tackle then New_Line; end if; end loop; New_line; end Test_Teams; ------------------------------------------------------------------ -- (1) Comparison with the previous loop shows that the for loop -- iteration scheme does not require 'Range. ------------------------------------------------------------------ |
The above program produces the following output:
Pitcher is the first baseball position Full_Back is the last football position First_Base precedes Second_Base Right_Tackle follows Right_Guard Right_End follows prior value 5 is the position # of Short_Stop Center_Field is at the 7th position Pitcher Catcher First_Base Second_Base Third_Base Short_Stop Center_Field Left_Field Left_End Left_Tackle Left_Guard Center Right_Guard Right_Tackle Right_End Quarterback Left_Halfback Right_Halfback Fullback |
Related Topics
[ Back to top of page ]