As I understand it, Ada uses 0 based indexes on its enumerated types.. So in Status_Type below, the ordinal value goes from 0 to 5.
type Status_Type is
(Undefined,
Available,
Fout,
Assigned,
Effected,
Cleared);
My question is.. what are the ordinal values for the following examples? Do they start at 0 or do they start from the ordinal value from the super type?
subtype Sub_Status_Type is Status_Type
range Available.. Effected;
subtype Un_Status_Type is Sub_Status_Type
range Fout .. Assigned;
Would Sub_Status_Type ordinal values go from 1 to 4 or from 0 to 3?
Would Un_Status_Type ordinal values go from 3 to 4 or from 1 to 2 or from 0 to 1?
For the subtypes, a
'poswill return the same value as it would have for the base type (1..4 and 2..3 respectively, I believe). Subtypes aren’t really new and different types, so much as they are the same old type, but with some extra limitations on its possible values.But it should be noted that these values are assigned under the scenes. It really should make no difference to you what they are, unless you are using the
'valand'posattributes, or you are interfacing to code written outside of Ada (or to hardware).Plus, if it does end up mattering, you should know that the situation is actually much more complicated.
'posand'valdon’t return the actual bit value the compiler uses for those enumeration values when it generates code. They just return their “ordinal position”; their offset from the first value.By default they will usually be the same thing. However, you can change the value assignments (but not the ordinal position assignments) yourself with a
for ... useclause, like in the code below: