comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: Some questions
Date: Thu, 10 Oct 2002 10:18:36 -0400
Date: 2002-10-10T10:18:36-04:00	[thread overview]
Message-ID: <uqb31t7anofoe8@corp.supernews.com> (raw)
In-Reply-To: 6c4a2b60cbe5ecf058fadca48f6c6e80.110780@mygate.mailgate.org


"Sim Con" <sicon@hotmail.com> wrote in message
news:6c4a2b60cbe5ecf058fadca48f6c6e80.110780@mygate.mailgate.org...
>
> I have a generic stack and i want to print it but the element (the
> generic part) could be integer or float so i think i can expand the
> instantiated generic class with a child with only the right print
> procedures, so i make this (for float, but the same for int):
[snip]
> package stack_float.print is
>     procedure Stampa(P: in Pila_int.pila);
> end stack_float.print;
> ------------------
>
> the compilator says "child of an instance must be an instance or
> renaming", so what's wrong? How can i build custom procedures for print
> the generic elements in generic packages???

A generic can only have generic children, that are in turn instantiated,
like this:

generic
   with function Image (Element : Element_Type) return String is <>;
procedure Stacks.Generic_Print (Stack : Stack_Type);

You then have to instantiate it as a child of the instantiation of the
parent, like this:

with Stacks.Generic_Print;
procedure Stack_Float.Print is new Stack_Float.Generic_Print (Float'Image);

Alternatively, if your stack has a passive iterator, like this:

generic
   type Element_Type is private;
package Stacks is

   type Stack_Type is limited private;

   generic
      with procedure Process (Element : Element_Type) is <>;
   procedure Generic_Select_Elements (Stack : Stack_Type);
...
end Stacks;

then you can do this:

S : Stack_Type;

declare
    procedure Process (F : Float) is
    begin
      Float_Text_IO.Put (F);  New_Line;
   end;

   procedure Print is new Generic_Select_Elements (Process);
begin
   Print (Stack);
end;

If your stack has an active iterator, then you can do something like:

S : Stack_Type;

declare
   I : Iterator_Type := First (S);
   J : constant Iterator_Type := Back (S);
begin
   while I /= J loop
      Float_Text_IO.Put (Element (I));  New_Line;
      I := Succ (I);
   end loop;
end;

So strictly speaking, you don't need a (generic) child procedure for
printing.

The Charles container library works like this (stack functionality is
provided by the sequence containers (vectors, deques, lists), rather than by
a specific stack abstraction).

http://home.earthlink.net/~matthewjheaney/charles/index.html







      parent reply	other threads:[~2002-10-10 14:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-10 11:17 Some questions Sim Con
2002-10-10 12:27 ` David C. Hoos
2002-10-10 13:34   ` Sim Con
2002-10-10 13:44     ` Colin Paul Gloster
2002-10-10 14:36     ` David C. Hoos
2002-10-10 14:18 ` Matthew Heaney [this message]
replies disabled

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