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

* Re: A little help on Generics.
  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-25  2:52 ` SteveD
  2 siblings, 0 replies; 8+ messages in thread
From: Dale Stanbrough @ 2002-06-24  7:34 UTC (permalink / raw)


Caffeine Junky wrote:

> 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.


It would help if you described what was wrong.


dale



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

* Re: A little help on Generics.
  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-25  2:52 ` SteveD
  2 siblings, 1 reply; 8+ messages in thread
From: Jeffrey Creem @ 2002-06-24 10:49 UTC (permalink / raw)



"Caffeine Junky" <nospam@hotmail.com> wrote in message
news:iOyR8.304072$cQ3.16530@sccrnsc01...
> 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.)
>
>    declare
>
>       type Int_Stack is new genstack(Max => 500, Item => Integer);
>
>

Since you did not post the body you may have other problems here but the
first I see is the
syntax here.  Try something like

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


Also, try to avoid the 'img attribute even in your test code..It is a handy
feature
but is not portable..It is a nice trick to have up your sleeve but it is
only a few
more kestrokes to type integer'image(ints(J)).

I there are a few more problems lurking in that test driver as well but I am
sure you will find these soon enough.






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

* Re: A little help on Generics.
  2002-06-24 10:49 ` Jeffrey Creem
@ 2002-06-24 18:50   ` Caffeine Junky
  2002-06-24 20:21     ` Jeffrey Creem
  0 siblings, 1 reply; 8+ messages in thread
From: Caffeine Junky @ 2002-06-24 18:50 UTC (permalink / raw)


On Mon, 24 Jun 2002 06:49:07 -0400, Jeffrey 

> Since you did not post the body you may have other problems here but the
> first I see is the
> syntax here.  Try something like
> 
> package Int_Stack is new genstack(Max => 500, Item => Integer);
> 
> 
> Also, try to avoid the 'img attribute even in your test code..It is a
> handy feature
> but is not portable..It is a nice trick to have up your sleeve but it is
> only a few
> more kestrokes to type integer'image(ints(J)).
> 
> I there are a few more problems lurking in that test driver as well but
> I am sure you will find these soon enough.
 
I dont seem to be having any problems with the body of the package, it
was just with the declaration of the package.

The only errors the compiler gave me told me that procedure Push was not
visible, Pop was not visible, etc...

However when running the compiler over just the package, it produces a
nice object file. Which leads me to beleive that my problem is in
calling the package from my test program rather than in the package
itself.

However, just to be sure, here is a copy of the body of the package...

package body genstack is

   procedure Push(X : in Item; S : in out Stack) is

   begin

      S := new Cell'(X, S);

   end Push;

   function Pop(S : in Stack) return Item is

      G : Item;

   begin

      G := S.Value;
      S := S.Next;
      return G;

   end Pop;

   function Is_Empty(S : in Stack) return Boolean is

   begin

      if S.Next = null then
         return True;
      else
         return False;
      end if;

   end Is_Empty;

end genstack;




Any pointers would be appreciated, as usual.


St4pL3



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

* Re: A little help on Generics.
  2002-06-24 18:50   ` Caffeine Junky
@ 2002-06-24 20:21     ` Jeffrey Creem
  2002-06-24 22:29       ` Caffeine Junky
  0 siblings, 1 reply; 8+ messages in thread
From: Jeffrey Creem @ 2002-06-24 20:21 UTC (permalink / raw)


Ok..In any case your first real problem is with the instantiation which
should look
like the one i gave you v.s. the one you used.

There are several things wrong with the body (not using Max, not actually
filling
in the next fields on pushes... a few others as well...)

This seems an aweful lot like a homework assignment...Is it?

"Caffeine Junky" <nospam@hotmail.com> wrote in message
news:ORJR8.298791$352.29149@sccrnsc02...
> On Mon, 24 Jun 2002 06:49:07 -0400, Jeffrey
>
> > Since you did not post the body you may have other problems here but the
> > first I see is the
> > syntax here.  Try something like
> >
> > package Int_Stack is new genstack(Max => 500, Item => Integer);
> >
> >
> > Also, try to avoid the 'img attribute even in your test code..It is a
> > handy feature
> > but is not portable..It is a nice trick to have up your sleeve but it is
> > only a few
> > more kestrokes to type integer'image(ints(J)).
> >
> > I there are a few more problems lurking in that test driver as well but
> > I am sure you will find these soon enough.
>
> I dont seem to be having any problems with the body of the package, it
> was just with the declaration of the package.
>
> The only errors the compiler gave me told me that procedure Push was not
> visible, Pop was not visible, etc...
>
> However when running the compiler over just the package, it produces a
> nice object file. Which leads me to beleive that my problem is in
> calling the package from my test program rather than in the package
> itself.
>
> However, just to be sure, here is a copy of the body of the package...
>
> package body genstack is
>
>    procedure Push(X : in Item; S : in out Stack) is
>
>    begin
>
>       S := new Cell'(X, S);
>
>    end Push;
>
>    function Pop(S : in Stack) return Item is
>
>       G : Item;
>
>    begin
>
>       G := S.Value;
>       S := S.Next;
>       return G;
>
>    end Pop;
>
>    function Is_Empty(S : in Stack) return Boolean is
>
>    begin
>
>       if S.Next = null then
>          return True;
>       else
>          return False;
>       end if;
>
>    end Is_Empty;
>
> end genstack;
>
>
>
>
> Any pointers would be appreciated, as usual.
>
>
> St4pL3





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

* Re: A little help on Generics.
  2002-06-24 20:21     ` Jeffrey Creem
@ 2002-06-24 22:29       ` Caffeine Junky
  0 siblings, 0 replies; 8+ messages in thread
From: Caffeine Junky @ 2002-06-24 22:29 UTC (permalink / raw)


On Mon, 24 Jun 2002 16:21:38 -0400, Jeffrey Creem wrote:

> Ok..In any case your first real problem is with the instantiation which
> should look
> like the one i gave you v.s. the one you used.
> 
> There are several things wrong with the body (not using Max, not
> actually filling
> in the next fields on pushes... a few others as well...)
> 
> This seems an aweful lot like a homework assignment...

No, it's not a homework assignment. I'm working out of Barnes's
"Programming in Ada95". I've basically stuck to C in the past, but now I
wanna learn Ada.

As far as the Push, this technique seems to work correctly in non-generic
packages.
Your correct about the Max parameter. I've just corrected that little
slip up.

Thanks for the pointers.


St4pL3



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

* Re: A little help on Generics.
  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-25  2:52 ` SteveD
  2002-06-25  7:25   ` Caffeine Junky
  2 siblings, 1 reply; 8+ messages in thread
From: SteveD @ 2002-06-25  2:52 UTC (permalink / raw)


To create an instance of the package:

> generic
>    Max : Positive;
>    type Item is private;
> package genstack is

Use:
  package Int_Stack is new genstack(Max => 500, Item => Integer);

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

Then you can use Int_Stack just a if it were a non-generic package.

You're close.  You are very very close.

SteveD






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

* Re: A little help on Generics.
  2002-06-25  2:52 ` SteveD
@ 2002-06-25  7:25   ` Caffeine Junky
  0 siblings, 0 replies; 8+ messages in thread
From: Caffeine Junky @ 2002-06-25  7:25 UTC (permalink / raw)


On Mon, 24 Jun 2002 22:52:31 -0400, SteveD wrote:

> To create an instance of the package:
> 
>> generic
>>    Max : Positive;
>>    type Item is private;
>> package genstack is
> 
> Use:
>   package Int_Stack is new genstack(Max => 500, Item => Integer);
> 
> Not:
>   type Int_Stack is new genstack(Max => 500, Item => Integer);
> 
> Then you can use Int_Stack just a if it were a non-generic package.
> 
> You're close.  You are very very close.
> 
> SteveD
 

I got it to work, more or less. The package instantiated just fine. And
the output also revealed some errors in my code.(Very embarassing errors
at that.)

I'll go over the code again, correct it, and post it up for scrutiny.


Thanks. I really appreciate it.

And, thanks for being patient. Heh.


Staple



^ 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