comp.lang.ada
 help / color / mirror / Atom feed
From: Caffeine Junky <nospam@hotmail.com>
Subject: A little help on Generics.
Date: Mon, 24 Jun 2002 06:15:42 GMT
Date: 2002-06-24T06:15:42+00:00	[thread overview]
Message-ID: <iOyR8.304072$cQ3.16530@sccrnsc01> (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



             reply	other threads:[~2002-06-24  6:15 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-24  6:15 Caffeine Junky [this message]
2002-06-24  7:34 ` A little help on Generics 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
replies disabled

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