comp.lang.ada
 help / color / mirror / Atom feed
* Some questions
@ 2002-10-10 11:17 Sim Con
  2002-10-10 12:27 ` David C. Hoos
  2002-10-10 14:18 ` Matthew Heaney
  0 siblings, 2 replies; 6+ messages in thread
From: Sim Con @ 2002-10-10 11:17 UTC (permalink / raw)


Hello! i have some questions, here i go:

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):

stack_float.ads
---------

with Stacks;
pragma elaborate_all(stacks);
package stack_float is new Stacks(float);
------------------

compilation ok, so problems come here:

stack_float_print.ads
---------

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

Thanxs in advance ^_^    


-- 
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG



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

* Re: Some questions
  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 14:18 ` Matthew Heaney
  1 sibling, 1 reply; 6+ messages in thread
From: David C. Hoos @ 2002-10-10 12:27 UTC (permalink / raw)



----- Original Message ----- 
From: "Sim Con" <sicon@hotmail.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Thursday, October 10, 2002 6:17 AM
Subject: Some questions


<snip>
> 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???
What you need to do is provide a print procedure when you instantiate the
generic.
This is done by declaring the generic like this:

generic
    type Item is private;
    with Procedure Print (I : Item) is <>;
package stacks
   type Stack is private;
.
   procedure Print (S : Stack);
.
end Stacks;

Then, when you instantiate the package, you do it specifying the type
if the items in the stack, and a print procedure for that type -- e.g.,:

package stack_float is new Stacks (float, print_float);

where print_float is your print procedure for the float type.

The <> in the generic declaration means that if there is a procedure
named Print visible at the point of instantiation, and having a parameter
profile matching that of the formal procedure parameter of your generic
declaration, then the instantiation can simply be
package stack_float is new Stacks (float);
because the matching print procedure will be included by default.

Then within your implementation of the generic package's Print procedure,
you can call the Print (I : Item) procedure for each item in the stack.






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

* Re: Some questions
  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
  0 siblings, 2 replies; 6+ messages in thread
From: Sim Con @ 2002-10-10 13:34 UTC (permalink / raw)


Thanxs for the answer ^_^ but i have another question: 
my print procedure needs a stack type for work and use some 
private variable of the generic class so if i define a specific print 
in other parts (main procedures or in other packages) i receive an
error, any help? thanx again!! ^_^


-- 
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG



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

* Re: Some questions
  2002-10-10 13:34   ` Sim Con
@ 2002-10-10 13:44     ` Colin Paul Gloster
  2002-10-10 14:36     ` David C. Hoos
  1 sibling, 0 replies; 6+ messages in thread
From: Colin Paul Gloster @ 2002-10-10 13:44 UTC (permalink / raw)


Sim Con wrote:
"[..] i have another question: 
my print procedure needs a stack type for work and use some 
private variable of the generic class so if i define a specific print 
in other parts (main procedures or in other packages) i receive an
error, any help? thanx again!!"

Add a function (an accessor method) to the generic which returns a copy of
the private variable. This function should not be private, so that it can
be called elsewhere.



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

* Re: Some questions
  2002-10-10 11:17 Some questions Sim Con
  2002-10-10 12:27 ` David C. Hoos
@ 2002-10-10 14:18 ` Matthew Heaney
  1 sibling, 0 replies; 6+ messages in thread
From: Matthew Heaney @ 2002-10-10 14:18 UTC (permalink / raw)



"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







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

* Re: Some questions
  2002-10-10 13:34   ` Sim Con
  2002-10-10 13:44     ` Colin Paul Gloster
@ 2002-10-10 14:36     ` David C. Hoos
  1 sibling, 0 replies; 6+ messages in thread
From: David C. Hoos @ 2002-10-10 14:36 UTC (permalink / raw)



----- Original Message ----- 
From: "Sim Con" <sicon@hotmail.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Thursday, October 10, 2002 8:34 AM
Subject: Re: Some questions


> Thanxs for the answer ^_^ but i have another question: 
> my print procedure needs a stack type for work and use some 
> private variable of the generic class so if i define a specific print 
> in other parts (main procedures or in other packages) i receive an
> error, any help? thanx again!! ^_^
Your _specific_ print procedure should only print an _item_ (e.g.,
a float).  Then inside your generic body you have the procedure
that uses the private variables of the generic class, calling your
specific via the formal generic parameter.

Alternatively, and probably better, instead of specifying a
specific _print_ procedure, you could specify an _image_ function
for each item.  That image function would return a string however
you want the item to appear when printed.  Then your generic
print procedure calls that _image_ function when it wants to show
an item.
 




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

end of thread, other threads:[~2002-10-10 14:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox