comp.lang.ada
 help / color / mirror / Atom feed
* using put procedure with a generic package
@ 2008-05-08  3:27 ophir.geffen
  2008-05-08  4:09 ` Jeffrey R. Carter
  0 siblings, 1 reply; 5+ messages in thread
From: ophir.geffen @ 2008-05-08  3:27 UTC (permalink / raw)


Hi
I'm trying to write a generic queue package with a Put procedure in it
like this...

* queues.ads:
with Ada.Text_IO;
generic
  type Item is private;
  with procedure Item_Put(The_Item : in Item);
package Queues is
  type Queue is private;
  ...
  procedure Put(The_Item : in Item); --- this uses Item_Put
  ...
end Queues;

* and use it in test_queues.adb:
with Ada.Text_IO, Queues;
use Ada.Text_IO;
procedure Test_Queues is
  package Int_IO is new Ada.Text_IO.Integer_IO(Integer);
  package Int_Queues is new Queues(Item => Integer, Item_Put =>
Int_IO.Put); --- error here
...
end Test_Queues;

The package compiles on its own, but when I try to compile Test_Queues
there is an error:
no visible subprogram matches the specification for "Item_Put"
What is the problem and how can I solve it?
The package worked when it was a non generic package and I had Item
defined as a private subtype.

Btw, I use Ada 95 with gnat.

Thanks in advance




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

* Re: using put procedure with a generic package
  2008-05-08  3:27 using put procedure with a generic package ophir.geffen
@ 2008-05-08  4:09 ` Jeffrey R. Carter
  2008-05-08  4:48   ` ophir.geffen
  0 siblings, 1 reply; 5+ messages in thread
From: Jeffrey R. Carter @ 2008-05-08  4:09 UTC (permalink / raw)


ophir.geffen@gmail.com wrote:
> 
> * queues.ads:
> with Ada.Text_IO;
> generic
>   type Item is private;
>   with procedure Item_Put(The_Item : in Item);

Item_Put has exactly one parameter.

>   package Int_IO is new Ada.Text_IO.Integer_IO(Integer);
>   package Int_Queues is new Queues(Item => Integer, Item_Put =>
> Int_IO.Put); --- error here
> 
> The package compiles on its own, but when I try to compile Test_Queues
> there is an error:
> no visible subprogram matches the specification for "Item_Put"

Ada.Text_IO.Integer_IO has the following procedures named Put:

procedure Put(File  : in File_Type;
               Item  : in Num;
               Width : in Field       := Default_Width;
               Base  : in Number_Base := Default_Base);

procedure Put(Item  : in Num;
               Width : in Field       := Default_Width;
               Base  : in Number_Base := Default_Base);

procedure Put(To   :    out String;
               Item : in     Num;
               Base : in     Number_Base := Default_Base);

None of these has exactly one parameter. That's why Int_IO.Put doesn't match 
Item_Put.

You can call the 2nd of these with only one explicit parameter:

Int_IO.Put (Item => 7);

but it still has 3 parameters; the other 2 get the default values as the actual 
parameters.

You need to create a procedure that matches Item_Put:

procedure Put (Item : in Integer) is
    -- null;
begin -- Put
    Int_IO.Put (Item => Item);
end Put;

and use it to instantiate your generic package.

-- 
Jeff Carter
"I unclog my nose towards you."
Monty Python & the Holy Grail
11



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

* Re: using put procedure with a generic package
  2008-05-08  4:09 ` Jeffrey R. Carter
@ 2008-05-08  4:48   ` ophir.geffen
  2008-05-08  7:24     ` Dmitry A. Kazakov
  2008-05-08 15:54     ` Adam Beneschan
  0 siblings, 2 replies; 5+ messages in thread
From: ophir.geffen @ 2008-05-08  4:48 UTC (permalink / raw)


On May 8, 7:09 am, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> wrote:
> ophir.gef...@gmail.com wrote:
>
> > * queues.ads:
> > with Ada.Text_IO;
> > generic
> >   type Item is private;
> >   with procedure Item_Put(The_Item : in Item);
>
> Item_Put has exactly one parameter.
>
> >   package Int_IO is new Ada.Text_IO.Integer_IO(Integer);
> >   package Int_Queues is new Queues(Item => Integer, Item_Put =>
> > Int_IO.Put); --- error here
>
> > The package compiles on its own, but when I try to compile Test_Queues
> > there is an error:
> > no visible subprogram matches the specification for "Item_Put"
>
> Ada.Text_IO.Integer_IO has the following procedures named Put:
>
> procedure Put(File  : in File_Type;
>                Item  : in Num;
>                Width : in Field       := Default_Width;
>                Base  : in Number_Base := Default_Base);
>
> procedure Put(Item  : in Num;
>                Width : in Field       := Default_Width;
>                Base  : in Number_Base := Default_Base);
>
> procedure Put(To   :    out String;
>                Item : in     Num;
>                Base : in     Number_Base := Default_Base);
>
> None of these has exactly one parameter. That's why Int_IO.Put doesn't match
> Item_Put.
>
> You can call the 2nd of these with only one explicit parameter:
>
> Int_IO.Put (Item => 7);
>
> but it still has 3 parameters; the other 2 get the default values as the actual
> parameters.
>
> You need to create a procedure that matches Item_Put:
>
> procedure Put (Item : in Integer) is
>     -- null;
> begin -- Put
>     Int_IO.Put (Item => Item);
> end Put;
>
> and use it to instantiate your generic package.
>
> --
> Jeff Carter
> "I unclog my nose towards you."
> Monty Python & the Holy Grail
> 11

Hi
Thanks for the quick reply

The problem is that the package needs to know how to handle most non-
limited types like Integer, String, Client, Plane, Book, Document, ...
The Put procedure in any of those might and should be different from
Put Integer.

And I don't want to force the package user to declare a special Put
procedure with only one var.
Is there a better way to write the package and/or a more correct way
to declare it when using it?




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

* Re: using put procedure with a generic package
  2008-05-08  4:48   ` ophir.geffen
@ 2008-05-08  7:24     ` Dmitry A. Kazakov
  2008-05-08 15:54     ` Adam Beneschan
  1 sibling, 0 replies; 5+ messages in thread
From: Dmitry A. Kazakov @ 2008-05-08  7:24 UTC (permalink / raw)


On Wed, 7 May 2008 21:48:47 -0700 (PDT), ophir.geffen@gmail.com wrote:

> The problem is that the package needs to know how to handle most non-
> limited types like Integer, String, Client, Plane, Book, Document, ...

String is an indefinite type.

> Is there a better way to write the package and/or a more correct way
> to declare it when using it?

That depends on what your queue package is. If it is a queue of items with

1. no requirements of the representation of in the queue

2. value copy semantics of insertion and retrieval

then I would use one of the following three variants:

1. Queue internally implemented by an array of elements

2. Queue implemented as a user-defined storage pool

3. Queue implemented as a user-defined stream

For any of these variants you just don't need any Put procedure. Copying is
predefined private type (cases 1 and 2). Stream attributes are also
predefined. The choice between 1 and 2 depends on whether Item is definite:

   type Item is private;  -- 1 would be OK
   type Item (<>) is private;  -- This is for the variant 2

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: using put procedure with a generic package
  2008-05-08  4:48   ` ophir.geffen
  2008-05-08  7:24     ` Dmitry A. Kazakov
@ 2008-05-08 15:54     ` Adam Beneschan
  1 sibling, 0 replies; 5+ messages in thread
From: Adam Beneschan @ 2008-05-08 15:54 UTC (permalink / raw)


On May 7, 9:48 pm, "ophir.gef...@gmail.com" <ophir.gef...@gmail.com>
wrote:
> On May 8, 7:09 am, "Jeffrey R. Carter"
>
>
>
> <spam.jrcarter....@spam.acm.org> wrote:
> > ophir.gef...@gmail.com wrote:
>
> > > * queues.ads:
> > > with Ada.Text_IO;
> > > generic
> > >   type Item is private;
> > >   with procedure Item_Put(The_Item : in Item);
>
> > Item_Put has exactly one parameter.

> Hi
> Thanks for the quick reply
>
> The problem is that the package needs to know how to handle most non-
> limited types like Integer, String, Client, Plane, Book, Document, ...
> The Put procedure in any of those might and should be different from
> Put Integer.
>
> And I don't want to force the package user to declare a special Put
> procedure with only one var.

I think you should anyway, even if you think you don't want to.  The
generic is defined correctly; the generic formal subprogram reflects
exactly what you need---an output routine that takes one parameter of
type Item.  In most cases, *somebody* is going to have to define a
routine like that, somewhere.  And if the package that defines, say,
Document, doesn't provide a routine with exactly that profile (maybe
the only Document output routines take page-dimension parameters),
then whoever instantiates your generic is going to have to write a
wrapper routine anyway.  It's pretty common in my experience to have
to write small routines like this for generic instantiation purposes,
and I don't see that changing until they decide to add something like
Lambda expressions to Ada, which I don't think will be happening soon.

In fact, if the Put routines of Integer_IO didn't have default
parameters (so that every time you called Put you would have to
explicitly specify the Width and Base), it would seem normal, and not
particularly burdensome, to write a one-parameter wrapper routine in
order to instantiate the generic.  I think only reason there's an
issue, and that you "don't want to" make the users declare "special"
Put routines, is that Put *looks* like it has just one parameter the
way it's normally used.  But since it doesn't, you're getting this
error which is surprising, and that makes it seem like users are being
forced to do something they shouldn't have to do.  Probably, the
solution is for the compiler to detect this case and display a more
helpful error message, but other than that I don't think this is a
problem.

                                  -- Adam






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

end of thread, other threads:[~2008-05-08 15:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-08  3:27 using put procedure with a generic package ophir.geffen
2008-05-08  4:09 ` Jeffrey R. Carter
2008-05-08  4:48   ` ophir.geffen
2008-05-08  7:24     ` Dmitry A. Kazakov
2008-05-08 15:54     ` Adam Beneschan

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