John Mayne
John Mayne

Reputation: 277

Ada: How to enumerate a type that consists of integers and other types?

For example, I want to create a type that represents all of the ranks of cards ( that is 2-10,Jack, Queen, King and Ace).

I thought of doing this:

    type Rank is (2,3,4,5,6,7,8,9,10,Jack,Queen,King,Ace);

But I get this error:

    identifier expected

Upvotes: 3

Views: 425

Answers (2)

Jacob Sparre Andersen
Jacob Sparre Andersen

Reputation: 6611

You could declare two helper types and a combined one:

package Mixed_Enumeration_And_Integer is
   type Integer_Values is range 1 .. 10;
   type Enumeration_Values is (Jack, Queen, King, Ace);

   type Object is private;

   function "+" (Item : Integer_Values) return Object;
   function "+" (Item : Enumeration_Values) return Object;

   function "+" (Item : Object) return Integer_Values;
   function "+" (Item : Object) return Enumeration_Values;

   function "=" (Left  : Integer_Values;
                 Right : Object) return Boolean;
   function "=" (Left  : Enumeration_Values;
                 Right : Object) return Boolean;

private
   type States is (Uninitialized, Integer, Enumeration);

   type Object (State : States := Uninitialized) is
      record
         case State is
            when Uninitialized => null;
            when Integer       => I : Integer_Values;
            when Enumeration   => E : Enumeration_Values;
         end case;
      end record;
end Mixed_Enumeration_And_Integer;

Upvotes: 2

Keith Thompson
Keith Thompson

Reputation: 263667

You can't.

The list in an enumeration type declaration consists of identifiers and/or character literals. You can't have integers literals in that context.

You can specify the values used to represent the enumerators using a representation clause, but I don't think that's what you want.

Just use identifiers:

type Rank is (R2,R3,R4,R5,R6,R7,R8,R9,R10,Jack,Queen,King,Ace);

Upvotes: 5

Related Questions