There is no general solution. The presentation of the enumeration view is apparently intended to make it difficult to obtain this information.
It:
function Rep is new Ada.Unchecked_Conversion(Enum, Integer);
most likely it will work in most cases, but there are some serious caveats: the view values ββshould be within the range of Integer'First..Integer'Last , and if the sizes of Enum and Integer do not match the result, the implementation is actually implemented (but it works with GNAT )
According to Simon Wright, RM recommends Unchecked_Conversion , but this is not a very satisfactory solution, and determining a consistent target type is difficult.
Since 2007, the recommended support level:
An implementation must support at least internal codes in the range System.Min_Int..System.Max_Int.
which means that converting to Integer not always enough; the value may be less than Integer'First or greater than Integer'Last . And even if all values ββare in this range, there is no really good way to determine the target type of the same size as the enumeration type. For example, this:
type Enum is (Ten, Twenty, Thirty); for Enum use (10, 20, 30); function Rep is new Ada.Unchecked_Conversion(Enum, Integer);
displays this warning in GNAT:
warning: types for unchecked conversion have different sizes
But after a warning, Rep returns the expected values ββof 10, 20, and 30.
RM explicitly states that if the source and target sizes in an Unchecked_Conversion instance do not match, and the result type is scalar, then
the result of the function is determined by the implementation and may have an invalid representation
Thus, the fact that the above works for GNAT does not mean that it is guaranteed to work everywhere.
For an implementation that only supports values ββin the range System.Min_Int..System.Max_Int , you can do something like this:
type Enum is (...); for Enum use (...); type Longest_Integer is range System.Min_Int .. System.Max_Int; function Rep is new Ada.Unchecked_Conversion(Enum, Longest_Integer);
and ignore the warning. But compilers are allowed to accept values ββthat exceed System.Max_Int if they are in the range of some integer type. For example, GNAT rejects this, but another Ada compiler may accept it:
type Longest_Unsigned is mod System.Max_Binary_Modulus; type Unsigned_Enum is (Zero, Huge); for Unsigned_Enum use (0, Longest_Unsigned'Last);
and Unchecked_Conversion from this to any signed integer type will not work. And you still have the potential problem of implementing certain results if the sizes do not match.
Here's a general solution that should work for any type of enumeration if (a) the view values ββare in the range System.Min_Int..System.Max_Int and (b) if it is better to implement Unchecked_Conversion behavior than the Ada standard, be:
type Longest_Signed is range System.Min_Int .. System.Max_Int; generic type Enum is (<>); function Generic_Rep(E: Enum) return Longest_Signed; function Generic_Rep(E: Enum) return Longest_Signed is function Rep is new Ada.Unchecked_Conversion(Enum, Longest_Signed); begin return Rep(E); end Generic_Rep;
Given all this confusion, you might consider using some kind of mechanism other than listing submission suggestions to do whatever you are trying to do.