Ada Programming/Attributes/'Pos

Description
The 'Pos attribute is defined for all discrete types. It is a function returning the argument's position number as a universal_integer; the prefix S must be a subtype name. (Universal integers are implicitly converted to any specific integer type as required by the context.)

S'Pos (Arg: S'Base) universal_integer;

For enumeration types, position numbers start at 0; if the argument does not denote a valid value (perhaps because of an uninitialized variable), the exception Program_Error is raised. For integer types, the attribute returns the value converted to universal_integer. Note that it is not necessary for the actual argument to belong to the subtype S.

Note that representation clauses do not affect position numbering. Whatever underlying value the enumerated value has, the position number will remain the same.

Example
I: Integer := -30; (Integer'Pos(I) = -30); My_Enum  (Enum1, Enum2, Enum3); My_Enum (Enum1 => 2, Enum2 => 4, Enum3 => 6); ... (My_Enum'Pos(Enum1) = 2); (My_Enum'Pos(Enum1) = 0); (My_Enum'Pos(Enum3) = 2); My_Enum_Sub My_Enum  Enum1 .. Enum2; (My_Enum_Sub'Pos (Enum3) = 2);

Another example without representation clause:

Color  (Red, Blue, White); Object : Color := White; Put (Color'Pos (Object)); -- prints 2, position of White. ...

Wikibook

 * Ada Programming
 * Ada Programming/Attributes/'Val
 * Ada Programming/Attributes/'Valid

Ada Reference Manual


|Pos