411 REFERENCE · Wikibooks
Ada Programming/Attributes/'Enum Rep
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Attributes 'Enum_Rep
__TOC__
Description
User_Enum_Type'Enum_Rep(Instance); where User_Enum_Type is an enumeration type and Instance is an instance of that type will return the underlying representation for that instance of the enumeration. The default representation of an enumeration is based on its position (starting at zero). However, Ada does provide language facilities for specifying the representation independently of the position. The Enum_Rep allows you to retrieve that representation. Generally in Ada, enumerations are their own type and the representation is not important. However, in the interests of cross language compatibility and for possible use in embedded programming the representation can be manipulated. While the core language allows you to change the representation, it doesn't provide a convenient attribute for retrieving it. This extended attribute addresses that need. Using it does require that you know the underlying type used to support the enumeration, since Enum_Rep returns that type. Typically, the standard type Integer is sufficient.
Note that this attribute is now standard in Ada 2022. In earlier Ada versions, it is available in GNAT as an implementation defined attribute, but the standard and portable way to get the internal representation was using an instantiation of Unchecked_Conversion.
Example
type Enum_Type is (Enum1, Enum2, Enum3);
Enum_Val : Enum_Type := Enum1;
Assert (Enum_Type'Enum_Rep(Enum_Val) = 0); OK
See also
Wikibook
Ada Programming
Ada Programming/Attributes
Ada Programming/Attributes/'Enum_Val
Ada Reference Manual
13 4 Enumeration Representation Clauses
K Language-Defined Attributes
Ada 2022 Overview
7.13 Getting the representation of an enumeration value
External links
GNAT Reference Manual > Implementation Defined Attributes > Enum_Rep