comp.lang.ada
 help / color / mirror / Atom feed
* A little help on Generics.
@ 2002-06-24  6:15 Caffeine Junky
  2002-06-24  7:34 ` Dale Stanbrough
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Caffeine Junky @ 2002-06-24  6:15 UTC (permalink / raw)


So far I've got Accesses and arrays and stuff figured out. I've kinda got
the package system figured out. But I'm having trouble figuring out how
to use Generics. More specifically, I know how to create a generic
package(at least the compiler doesnt give me any errors when making an
object file).
I just dont know the proper way to instantiate it.

Heres a package spec and a test file that you guys can peak at and
hopefully give me some hints as to what I'm doing wrong.

(Please be kind, I'm still a newbie. Heh.)

generic
   Max : Positive;
   type Item is private;

package genstack is
-- I created this package so that I could practice using --
-- Generics.						 --
-- Nothing fancy here.					 --

   type Stack is limited private;

   procedure Push(X : in Item; S : in out Stack);
   function Pop(S: in Stack) return Item;
   function Is_Empty(S : in Stack) return Boolean;

private

   type Cell;
   type Stack is access Cell;
   type Cell is
      record
         Value: Item;
         Next: Stack;
      end record;

   -- This package will be expanded to include other kinds of stacks --
   -- as time permits.                                               --

end genstack;

That's the specification. Everything looks good to my untrained eye. How
about yours?


And this is the test/practice code I'm using...


with Text_IO;
with genstack;

procedure test is

   Alphs : constant String := "ABCDEFGHIJKLMNOP";

   Ints : array(Integer range 0..500) of Integer;

begin



   declare

      type Int_Stack is new genstack(Max => 500, Item => Integer);

      use Int_Stack;
      Ret: Integer;
      Some_Ints: Stack;
   begin
      -- Initialize the Integer array here. --

      for Z in Ints'Range loop
         Ints(Z) := Z;
      end loop;

      for J in Ints'Range loop
         Text_IO.Put_Line("Pushing number"& Ints(J)'Img &"onto the stack");
         Push(Ints(J), Some_Ints);
      end loop;

      Text_IO.Put_Line("Finished Pushing all numbers onto the stack.");
      Text_IO.New_Line;

      for D in Ints'Range loop
         Ret := Pop(Some_Ints);
         Text_IO.Put_Line("Popped"& Ret'Img &" from the stack.");
      end loop;

   end;

   declare

      type Alph_Stack is new genstack(Max => 16, Item => Character);
      Letters : String(1..16);
      use Alph_Stack;
      A_Word : Alph_Stack;
   begin

      Text_IO.Put_Line("Pushing Letters onto the stack.");

      for Y in Alphs'Range loop
         Push(Alph(Y), A_Word);
      end loop;

      Text_IO.Put_Line("Finished Pushing letters onto the stack.");

      Text_IO.New_Line;

      Text_IO.Put_Line("Now Popping the letters from the stack");

      for N in Alphs'Range loop
         Letters(N) := Pop(A_Word);
         Text_IO.Put_Line("Popped letter"& Letters(N) &"from the Stack");
      end loop;

   end;


end test;



Any help would be appreciated.

Thanks


St4pL3



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

end of thread, other threads:[~2002-06-25  7:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-06-24  6:15 A little help on Generics Caffeine Junky
2002-06-24  7:34 ` Dale Stanbrough
2002-06-24 10:49 ` Jeffrey Creem
2002-06-24 18:50   ` Caffeine Junky
2002-06-24 20:21     ` Jeffrey Creem
2002-06-24 22:29       ` Caffeine Junky
2002-06-25  2:52 ` SteveD
2002-06-25  7:25   ` Caffeine Junky

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