comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <nholsti@icon.fi>
Subject: Re: Generics and I/O
Date: 1998/10/03
Date: 1998-10-03T00:00:00+00:00	[thread overview]
Message-ID: <3615F6F6.DD1D222D@icon.fi> (raw)
In-Reply-To: Pine.GSO.3.96L.981002182724.301A-100000@unixs1.cis.pitt.edu

William L Hayhurst wrote:
> 
> Hi there.  I have a question about Generics and IO.  Given
> 
> generic
>     type Item is private;
>     package Foo is
>        -- ... --
>        procedure Print is
>            -- ... --
>            Put( Item );
>            -- ... --
>        end Print;
>     end Foo;
> 
> My compiler ( GNAT ) will flag the Put( Item ) as an error.

  [ rest snipped ]

GNAT is correct; no Put operation is visible where you try to call it,
in
the generic. You need to supply a Put. Since the Item type is private,
you
should include this Put as a generic formal parameter:

   generic
      type Item is private;
      with procedure Put (I : in Item);
   package Foo is
      procedure Print (I : in Item);
   end Foo;

Then, when you instantiate your generic, you supply the Put operation
to be used, as shown in the code example below. I've used an enumeration
type for Item, but I hope you get the point and can do the same for
Integer.

Note that it's not necessary to have a "wrapper" operation like MyPut.
By placing the proper specification for "Put" in the generic part, you
could instantiate Foo with MyItem_IO.Put directly. However, the Text_IO
Put operations have additional parameters, such as Width, that must be
included in the generic Foo Put specification for this to work.
The instantiation can also find a suitable Put "automagically" by using
the "<>" symbol in the generic spec, but I won't go into that.

Hope this helps,
- Niklas


with Foo;
with Text_IO;

procedure Main is

   type MyItem is (This, That, TheOther);

   package MyItem_IO is new Text_IO.Enumeration_IO (MyItem);

   procedure MyPut (I : in MyItem) is
   begin
      Text_IO.Put ("MyPut: ");
      MyItem_IO.Put (I);
      Text_IO.New_Line;
   end MyPut;

   package MyFoo is new Foo (MyItem, MyPut);

begin
   MyFoo.Print (That);
end Main;




  reply	other threads:[~1998-10-03  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-10-02  0:00 Generics and I/O William L Hayhurst
1998-10-03  0:00 ` Niklas Holsti [this message]
1998-10-03  0:00 ` Dmitriy Anisimkov
1998-10-06  0:00 ` Robert I. Eachus
1998-10-07  0:00   ` Dale Stanbrough
replies disabled

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