comp.lang.ada
 help / color / mirror / Atom feed
* array(enumeration)
@ 2000-07-19  0:00 Mario Amado Alves
  2000-07-20  0:00 ` array(enumeration) Philip Anderson
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Mario Amado Alves @ 2000-07-19  0:00 UTC (permalink / raw)
  To: comp.lang.ada

How do I define an array type indexed by an enumeration type?

  type Array_Type is array(<>) of Some_Type;
    --                    /|\
    --                     |
    -- compiler says identifier expected here

  type Enumeration_Type is (A, B, C);
  type Another_Enumeration_Type is (X, Y, Z);

  A_Thing: Array_Type(Enumeration_Type) := (
    A => ... ,
    B => ... ,
    C => ... );

  Another_Thing: Array_Type(Another_Emuneration_Type) := (
    X => ... ,
    Y => ... ,
    Z => ... );

This makes sense to me, but does not compile. So how do I code the idea?

Thanks.

| |,| | | |RuaFranciscoTaborda24RcD 2815-249CharnecaCaparica 351+939354002
|M|A|R|I|O|
|A|M|A|D|O|DepartmentoDeInformaticaFCT/UNL 2825-114 Caparica 351+212958536
|A|L|V|E|S|                                                  fax 212948541
| | | | | |                 maa@di.fct.unl.pt                FCT 212948300









^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: array(enumeration)
  2000-07-19  0:00 array(enumeration) Mario Amado Alves
  2000-07-20  0:00 ` array(enumeration) Philip Anderson
  2000-07-20  0:00 ` array(enumeration) Steve Folly
@ 2000-07-20  0:00 ` Frode Tenneboe
  2 siblings, 0 replies; 4+ messages in thread
From: Frode Tenneboe @ 2000-07-20  0:00 UTC (permalink / raw)


Mario Amado Alves <maa@di.fct.unl.pt> wrote:
: How do I define an array type indexed by an enumeration type?

:   type Array_Type is array(<>) of Some_Type;
:     --                    /|\
:     --                     |
:     -- compiler says identifier expected here

It is. You have to give the discrete type otherwise integer is assumed.

Try:

with Text_Io;

procedure Enum is

   type Month is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
   type Array_Type is array(Month range <>) of Integer;

   Spring : Array_Type(Mar..May) := (Mar => 31, Apr => 30, May =>31);

begin
   Text_Io.Put_Line(Month'Image(Spring'First)& " has "& Integer'Image(Spring(Spring'First))& " days.");

end Enum;

: This makes sense to me, but does not compile. So how do I code the idea?

Does this make more sense?

 -Frode
-- 
^ Frode Tenneb�                    | email: ft@edh.ericsson.se      ^
| Ericsson Radar AS. N-1788 Halden |                                |
| Phone: +47 69 21 41 47           | Frode@IRC                      |
| with Standard.Disclaimer; use Standard.Disclaimer;                |




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: array(enumeration)
  2000-07-19  0:00 array(enumeration) Mario Amado Alves
@ 2000-07-20  0:00 ` Philip Anderson
  2000-07-20  0:00 ` array(enumeration) Steve Folly
  2000-07-20  0:00 ` array(enumeration) Frode Tenneboe
  2 siblings, 0 replies; 4+ messages in thread
From: Philip Anderson @ 2000-07-20  0:00 UTC (permalink / raw)


Mario Amado Alves wrote:
> 
> How do I define an array type indexed by an enumeration type?
> 
>   type Array_Type is array(<>) of Some_Type;
>     --                    /|\
>     --                     |
>     -- compiler says identifier expected here

Yes, you need to fully define the type, including the type of the index.


>   type Enumeration_Type is (A, B, C);
>   type Another_Enumeration_Type is (X, Y, Z);
> 
>   A_Thing: Array_Type(Enumeration_Type) := (
>     A => ... ,
>     B => ... ,
>     C => ... );
> 
>   Another_Thing: Array_Type(Another_Emuneration_Type) := (
>     X => ... ,
>     Y => ... ,
>     Z => ... );
> 
> This makes sense to me, but does not compile. So how do I code the idea?


type Array_Type is array (Enumeration_Type) of Some_Type;

type Another_Array_Type is array (Another_Enumeration_Type) of
Some_Type;


A_Thing: Array_Type := (
   A => ... ,
   B => ... ,
   C => ... );

Another_Thing: Another_Array_Type := (
   X => ... ,
   Y => ... ,
   Z => ... );

Note that these objects are of different types, and cannot be assigned
to each other even though the item type is the same.


or simply:

  A_Thing : array (Enumeration_Type) of Some_Type := (


-- 
hwyl/cheers,
Philip Anderson
Alenia Marconi Systems
Cwmbr�n, Cymru/Wales




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: array(enumeration)
  2000-07-19  0:00 array(enumeration) Mario Amado Alves
  2000-07-20  0:00 ` array(enumeration) Philip Anderson
@ 2000-07-20  0:00 ` Steve Folly
  2000-07-20  0:00 ` array(enumeration) Frode Tenneboe
  2 siblings, 0 replies; 4+ messages in thread
From: Steve Folly @ 2000-07-20  0:00 UTC (permalink / raw)


On Wed, 19 Jul 2000 17:13:44 +0100 (WEST), Mario Amado Alves <maa@di.fct.unl.pt>
did clatter that keyboard and type:

>How do I define an array type indexed by an enumeration type?
>
>  type Array_Type is array(<>) of Some_Type;
>    --                    /|\
>    --                     |
>    -- compiler says identifier expected here
>
>  type Enumeration_Type is (A, B, C);
>  type Another_Enumeration_Type is (X, Y, Z);
>
>  A_Thing: Array_Type(Enumeration_Type) := (
>    A => ... ,
>    B => ... ,
>    C => ... );
>
>  Another_Thing: Array_Type(Another_Emuneration_Type) := (
>    X => ... ,
>    Y => ... ,
>    Z => ... );
>
>This makes sense to me, but does not compile. So how do I code the idea?
>

You have to create separate array types for each enumeration...

	type Enum_Type is (A, B, C);
	type Enum_Array is array (Enum_Type) of Some_Type;

	type Another_Enum_Type is (X, Y, Z);
	type Another_Enum_Array is array (Another_Enum_Type) of Some_Type;
	
	A_Thing : Enum_Array := ....
	Another_Thing : Another_Enum_Array := ...

OK, the names get a bit long winded, but you can easily sort that out!

I suppose you could make a generic package and export the array type from it,
but I really can't see any advantage in doing it. More (unnecessary) code, for a
start!

generic
   type Index is (<>);
   type Item is private;
package Generic_Array is
   type Array is array (Index) of Item;
end Generic_Array;

and then...

package Enum_Array is new Generic_Array(Index => Enumeration_Type, Item =>
Some_Type);

A_Thing : Enum_Array.Array := ....


 If were thinking of maybe adding a few subprograms to do clever things to the
array (iterators, maybe), then maybe you could go down this route, otherwise I
would suggest just sticking to plain types. Don't overcomplicate things!


-- 
Regards,
Steve Folly.




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2000-07-20  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-07-19  0:00 array(enumeration) Mario Amado Alves
2000-07-20  0:00 ` array(enumeration) Philip Anderson
2000-07-20  0:00 ` array(enumeration) Steve Folly
2000-07-20  0:00 ` array(enumeration) Frode Tenneboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox