5

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?

3
  • It's generally best to post your actual code, copy-and-pasted from your source file. How do I know you didn't? Because out is a reserved word. Sep 18, 2011 at 2:09
  • ... How does that change anything... Sep 18, 2011 at 21:18
  • It probably doesn't in this case, but I've seen plenty of questions where the relevant problem was invisible because the poster re-typed the code rather than copy-and-pasting it. It's also helpful if someone wants to compile your code to test it. Sep 18, 2011 at 21:31

4 Answers 4

9

For the subtypes, a 'pos will 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 'val and 'pos attributes, 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. 'pos and 'val don'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 ... use clause, like in the code below:

for Status_Type use
 (Undefined => 1,  
  Available => 2,
  Out       => 4,     
  Assigned  => 8,     
  Effected  => 16,  
  Cleared   => 32); 
2
  • But an enumeration representation clause doesn't affect the behavior of 'Pos; Status_Type'Pos(Cleared) will still be 5. Unless something has been added to the language since the last time I looked, there's no clean way to get the representation number for an enumeration constant (Unchecked_Conversion can probably do it, but determining the appropriate target type is non-trivial). Sep 7, 2011 at 19:27
  • True. It matters for extra-language and H/W interfaces, but not 'Pos/'Val. I'll try to clarify, if I can find a way to do it w/o doubling the size of my answer.
    – T.E.D.
    Sep 7, 2011 at 21:23
5

The position number is defined in terms of the base type. So Sub_Status_Type'Pos(Assigned) is the same as Status_Type'Pos(Assigned), and the position values of Sub_Status_Type go from 1 to 4, not 0 to 3.

(And note that the position number isn't affected by an enumeration representation clause; it always starts at 0 for the first value of the base type.)

Incidentally, it would have been easy enough to find out by running a small test program that prints the values of Sub_Status_Type'Pos(...) -- which would also have told you that you can't use the reserved word out as an identifier.

2

As I understand it, Ada uses 0 based indexes on its enumerated types

Yes, it uses 0 for the indexes, or rather for the position of the values of the type. This is not the value of the enumeration literals, and not the binary representation of them.

what are the ordinal values for the following examples?

There are no "ordinal" values. The values of the type are the ones you specified. You are confusing "value", "representation", and "position" here.

The values of your Status_Type are Undefined, Available, Out, Assigned, Effected, and Cleared.

The positions are 0, 1, 2, 3, 4, and 5. These are what you can use to translate with 'Pos and 'Val.

The representation defaults to the position, but you can freely assign other values (as long as you keep the correct order). These are used if you write it to a file, or send it through a socket, or load it into a register..

2
1

I think the best way to answer your questions is in reverse:

A subtype is, mathematically speaking, a continuous subset of its parent type. So, if the type SIZES is (1, 2, 3, 4, 5, 6, 7, 8) and you define a subtype MEDIUM as (4,5) the first element of MEDIUM is 4. Example:

Type Small_Natural is 0..16;
Subtype Small_Positive is Small_Natural'Succ(Small_Natural'First)..Small_Natural'Last;

This defines two small sets of possible-values, which are tightly related: namely that Positive numbers are all the Natural Numbers save Zero.

I used this form to illustrate that with a few text-changes we have the following example:

Type Device is ( Not_Present, Power_Save, Read, Write );
Subtype Device_State is Device'Succ(Device'First)..Device'Last;

And here we are modeling the intuitive notion that a device must be present to have a state, but note that the values in the subtype ARE [exactly] the values in the type from which they are derived.

This answers your second question: Yes, an element of an enumeration would have the same value that its parent-type would.

As to the first, I believe the starting position is actually implementation defined (if not then I assume the LM defaults it to 0). You are, however free to override that and provide your own numbering, the only restriction being that elements earlier in the enumeration are valued less than the value that you are assigning currently [IIRC].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.