comp.lang.ada
 help / color / mirror / Atom feed
* array of generic packages?
@ 2004-09-22 18:03 a malkiel
  2004-09-22 18:26 ` Ludovic Brenta
  2004-09-22 23:30 ` Stephen Leake
  0 siblings, 2 replies; 6+ messages in thread
From: a malkiel @ 2004-09-22 18:03 UTC (permalink / raw)


hi im somewhat new to ada. i've been trying to nut this one but can't seem 
to. Would anyone know if defining an "array of generic packages" is 
possible? What im looking for is something of the nature:

type shape_type is (circle, triangle);
package shapePointer(1) is new shape(circle);
package shapePointer(2) is new shape(triangle);

thanks in advance.
ciao, andrew 





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

* Re: array of generic packages?
  2004-09-22 18:03 array of generic packages? a malkiel
@ 2004-09-22 18:26 ` Ludovic Brenta
  2004-09-23  2:19   ` a malkiel
  2004-09-22 23:30 ` Stephen Leake
  1 sibling, 1 reply; 6+ messages in thread
From: Ludovic Brenta @ 2004-09-22 18:26 UTC (permalink / raw)


"a malkiel" writes:
> hi im somewhat new to ada. i've been trying to nut this one but can't seem 
> to. Would anyone know if defining an "array of generic packages" is 
> possible? What im looking for is something of the nature:
>
> type shape_type is (circle, triangle);
> package shapePointer(1) is new shape(circle);
> package shapePointer(2) is new shape(triangle);
>
> thanks in advance.
> ciao, andrew 

No, you cannot have an array of generic packages.  You can only have
arrays of objects; objects are constants and variables.  You can
create a generic package that declares a new type of arrays, like so:

generic
   type Element_Type is private;
package Generic_Arrays is
   type Array_Type is array (Positive range <>) of Element_Type;
end Generic_Arrays;

type Triangle is ...

package Arrays_Of_Triangles is
    new Generic_Arrays (Element_Type => Triangle);

My_Triangle : Triangle;
My_Array : Arrays_Of_Triangles.Array_Type := (1 => My_Triangle);

However, the generic package is not really all that useful if all you
want is to declare new array types.  You'd be better off doing just:

type Array_Of_Triangles is array (Positive range <>) of Triangle;
type Array_Of_Circles is array (Positive range <>) of Circle;

which does not require a generic.

-- 
Ludovic Brenta.



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

* Re: array of generic packages?
  2004-09-22 18:03 array of generic packages? a malkiel
  2004-09-22 18:26 ` Ludovic Brenta
@ 2004-09-22 23:30 ` Stephen Leake
  2004-09-23  2:15   ` a malkiel
  1 sibling, 1 reply; 6+ messages in thread
From: Stephen Leake @ 2004-09-22 23:30 UTC (permalink / raw)
  To: comp.lang.ada

"a malkiel" <malkiel@wannet.net> writes:

> hi im somewhat new to ada. i've been trying to nut this one but can't seem 
> to. Would anyone know if defining an "array of generic packages" is 
> possible? What im looking for is something of the nature:
> 
> type shape_type is (circle, triangle);
> package shapePointer(1) is new shape(circle);
> package shapePointer(2) is new shape(triangle);

This is not directly possible.

But perhaps you are looking for tagged types:

type Shape_Type is tagged abstract limited private;
type Shape_Access_Type is access all Shape_Type;

procedure Draw (Item : in Shape_Type);

type Circle_Type is new Shape_Type with private;

procedure Draw (Item : in Circle_Type);

function New_Circle (Radius : in Float) return Shape_Access_Type;

type Triangle_Type is new Shape_Type with private;

procedure Draw (Item : in Triangle_Type);

function New_Triangle (A, B, C : in Float) return Shape_Access_Type;

Then you can do things like:

type Shape_Array_Type is array (...) of Shape_Access_Type;

My_Shapes : Shape_Array_Type :=
  (New_Circle (2.0),
   New_Triangle (3.0, 4.0, 5.0));


for I in My_Shapes'range loop
   Draw (My_Shapes (I).all);
end loop;


If that's not what you want, perhaps you could say more about what you
do want, and we could talk about how to do it "the Ada way".

-- 
-- Stephe




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

* Re: array of generic packages?
  2004-09-22 23:30 ` Stephen Leake
@ 2004-09-23  2:15   ` a malkiel
  2004-09-24 12:39     ` Peter Hermann
  0 siblings, 1 reply; 6+ messages in thread
From: a malkiel @ 2004-09-23  2:15 UTC (permalink / raw)


Thanks for the response! Actually tagged types look the way to go. There 
seem to be endless wonders to ADA. Again thanks for the exhaustive response!

Andy 





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

* Re: array of generic packages?
  2004-09-22 18:26 ` Ludovic Brenta
@ 2004-09-23  2:19   ` a malkiel
  0 siblings, 0 replies; 6+ messages in thread
From: a malkiel @ 2004-09-23  2:19 UTC (permalink / raw)


thanks for that! its a shame i cant create an array of packages... I have 
reorganised my code and have used something along the lines of what you 
suggested (inclusive of tagged types). cheers! Andy 





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

* Re: array of generic packages?
  2004-09-23  2:15   ` a malkiel
@ 2004-09-24 12:39     ` Peter Hermann
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Hermann @ 2004-09-24 12:39 UTC (permalink / raw)


a malkiel <malkiel@wannet.net> wrote:
> seem to be endless wonders to ADA. Again thanks for the exhaustive response!

guess what you still have to learn?
(-: every Ada fan knows :-)

have a nice weekend

-- 
--Peter Hermann(49)0711-685-3611 fax3758 ica2ph@csv.ica.uni-stuttgart.de
--Pfaffenwaldring 27 Raum 114, D-70569 Stuttgart Uni Computeranwendungen
--http://www.csv.ica.uni-stuttgart.de/homes/ph/
--Team Ada: "C'mon people let the world begin" (Paul McCartney)



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

end of thread, other threads:[~2004-09-24 12:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-22 18:03 array of generic packages? a malkiel
2004-09-22 18:26 ` Ludovic Brenta
2004-09-23  2:19   ` a malkiel
2004-09-22 23:30 ` Stephen Leake
2004-09-23  2:15   ` a malkiel
2004-09-24 12:39     ` Peter Hermann

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