Ada Programming/Types/Enumeration
An enumeration type is defined as a list of possible values:
type
Primary_Coloris
(Red, Green, Blue);
Like for numeric types, where e.g. 1 is an integer literal, Red, Green and Blue are called the literals of this type. There are no other values assignable to objects of this type.
Operators and attributes
[edit | edit source]Apart from equality ("="), the only operators on enumeration types are the ordering operators: "<", "<=", "=", "/=", ">=", ">", where the order relation is given implicitly by the sequence of literals: Each literal has a position, starting with 0 for the first, incremented by one for each successor. This position can be queried via the 'Pos attribute; the inverse is 'Val, which returns the corresponding literal. In our example:
Primary_Color'Pos (Red) = 0 Primary_Color'Val (0) = Red
There are two other important attributes: Image and Value (don't confuse Val with Value). Image returns the string representation of the value (in capital letters), Value is the inverse:
Primary_Color'Image ( Red ) = "RED" Primary_Color'Value ("Red") = Red
These attributes are important for simple IO (there are more elaborate IO facilities in Ada.Text_IO for enumeration types). Note that, since Ada is case-insensitive, the string given to 'Value can be in any case.
Enumeration literals
[edit | edit source]Literals are overloadable, i.e. you can have another type with the same literals.
type
Traffic_Lightis
(Red, Yellow, Green);
Overload resolution within the context of use of a literal normally resolves which Red is meant. Only if you have an unresolvable overloading conflict, you can qualify with special syntax which Red is meant:
Primary_Color'(Red)
Like many other declarative items, enumeration literals can be renamed. In fact, such a literal is actually a function, so it has to be renamed as such:
function
Redreturn
P.Primary_Colorrenames
P.Red;
Here, Primary_Color is assumed to be defined in package P, which is visible at the place of the renaming declaration. Renaming makes Red directly visible without necessity to resort the use-clause.
Note that redeclaration as a function does not affect the staticness of the literal.
Characters as enumeration literals
[edit | edit source]Rather unique to Ada is the use of character literals as enumeration literals:
type
ABCis
('A', 'B', 'C');
This literal 'A' has nothing in common with the literal 'A' of the predefined type Character (or Wide_Character).
Every type that has at least one character literal is a character type. For every character type, string literals and the concatenation operator "&" are also implicitly defined.
type
My_Characteris
(No_Character, 'a', Literal, 'z');type
My_Stringis
array
(Positiverange
<>)of
My_Character; S: My_String := "aa" & Literal & "za" & 'z'; T: My_String := ('a', 'a', Literal, 'z', 'a', 'z');
In this example, S and T have the same value.
Ada's Character type is defined that way. See Ada Programming/Libraries/Standard.
Booleans as enumeration literals
[edit | edit source]Also Booleans are defined as enumeration types:
type
Booleanis
(False, True);
There is special semantics implied with this declaration in that objects and expressions of this type can be used as conditions. Note that the literals False and True are not Ada keywords.
Thus it is not sufficient to declare a type with these literals and then hope objects of this type can be used like so:
type
My_Booleanis
(False, True); Condition: My_Boolean;if
Conditionthen
-- wrong, won't compile
If you need your own Booleans (perhaps with special size requirements), you have to derive from the predefined Boolean:
type
My_Booleanis
new
Boolean; Condition: My_Boolean;if
Conditionthen
-- OK
Enumeration subtypes
[edit | edit source]You can use range
to subtype an enumeration type:
subtype
Capital_Letteris
Characterrange
'A' .. 'Z';
type
Day_Of_Weekis
(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);subtype
Working_Dayis
Day_Of_Weekrange
Monday .. Friday;
Using enumerations
[edit | edit source]Enumeration types being scalar subtypes, type attributes such as First and Succ will allow stepping through a subsequence of the values.
case
Day_Of_Week'Firstis
when
Sunday => ISO (False);when
Day_Of_Week'Succ (Sunday) => ISO (True);when
Tuesday .. Saturday =>raise
Program_Error;end
case
;
A loop will automatically step through the values of the subtype's range. Filtering week days to include only working days with an even position number:
for
Dayin
Working_Dayloop
if
Day_Of_Week'Pos (Day)mod
2 = 0then
Work_In_Backyard;end
if
;end
loop
;
Enumeration types can be used as array index subtypes, yielding a table feature:
type
Officer_IDis
range
0 .. 50;type
Scheduleis
array
(Working_Day)of
Officer_ID;
See also
[edit | edit source]Wikibook
[edit | edit source]- Ada Programming
- Ada Programming/Types
- Ada Programming/Libraries/Standard
- Ada_Programming/Attributes/'First
- Ada_Programming/Attributes/'Last
- Ada_Programming/Attributes/'Pred
- Ada_Programming/Attributes/'Succ
- Ada_Programming/Attributes/'Img
- Ada_Programming/Attributes/'Image
- Ada_Programming/Attributes/'Value
- Ada_Programming/Attributes/'Pos
- Ada_Programming/Attributes/'Val
- Ada_Programming/Attributes/'Enum_Rep
- Ada_Programming/Attributes/'Enum_Val