Ada Programming/Attributes/'Val

Description
The 'Val attribute is defined for all discrete types. It is a function returning that value of the base type of S having the argument's position number; the prefix S must be a subtype name. (Any specific integer type is implicitly converted to universal_integer.)

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

If no value of the type of S has the position number Arg, the Constraint_Error exception is raised.

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

Example
My_Enum (Enum1, Enum2, Enum3); My_Other_Enum (Value1, Value2, Value3); My_Other_Enum (Value1 => 2, Value2 => 4, Value3 => 6); (My_Enum'Val(0) = Enum1); (My_Enum'Val(1) = Enum2); (My_Enum'Val(2) = Enum3); (My_Other_Enum'Val(0) = Value1); (My_Other_Enum'Val(1) = Value2); (My_Other_Enum'Val(2) = Value3); (My_Enum'Val(3)      = Enum3); (My_Other_Enum'Val(2) = Value1); (My_Other_Enum'Val(4) = Value2); (My_Other_Enum'Val(6) = Value3);

Other example:

Color (RED, BLUE, WHITE); OBJECT : Color := WHITE; Put (Color'Val (1));

The internal representation can only be queried via Unchecked_Conversion.

Wikibook

 * Ada Programming
 * Ada Programming/Attributes
 * Ada Programming/Attributes/'Pos

Ada Reference Manual


|Val