comp.lang.ada
 help / color / mirror / Atom feed
* Generics and I/O
@ 1998-10-02  0:00 William L Hayhurst
  1998-10-03  0:00 ` Niklas Holsti
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: William L Hayhurst @ 1998-10-02  0:00 UTC (permalink / raw)


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.
So if I instantiate foo as

declare
  -- ... --
  with Foo; use Foo;
  package Int_Foo is new Foo ( Item => Integer );
  use Int_Foo;

  -- ... -- 
end; 

and the Put( ) still won't work, even if a declare a new
Int_IO package in the scope of Foo::Print.  So do I have to
overload Put( ) inside package foo? 

Thanks in advance!

--Lyle






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

* Re: Generics and I/O
  1998-10-02  0:00 Generics and I/O William L Hayhurst
  1998-10-03  0:00 ` Niklas Holsti
@ 1998-10-03  0:00 ` Dmitriy Anisimkov
  1998-10-06  0:00 ` Robert I. Eachus
  2 siblings, 0 replies; 5+ messages in thread
From: Dmitriy Anisimkov @ 1998-10-03  0:00 UTC (permalink / raw)


if you Item is a scalar type then :
with Text_IO;
> generic
>     type Item is private;
>     package Foo is
>        -- ... --
        procedure Print(Value : Item) is
>            -- ... --
            Text_IO.Put( Value'Image );
>            -- ... --
>        end Print;
>     end Foo;

if you Item is only integer type then :
with Ada.Text_IO.Integer_IO;
> generic
type Item is range <>; -- must be
>     package Foo is
>        -- ... --
package Item_IO is new Ada.Text_IO.Integer_IO(Item); -- instantiation
use Item_IO;
       procedure Print(Value : Item) is -- must be
>            -- ... --
            Put( Value ); -- must be
>            -- ... --
>        end Print;
>     end Foo;




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

* Re: Generics and I/O
  1998-10-02  0:00 Generics and I/O William L Hayhurst
@ 1998-10-03  0:00 ` Niklas Holsti
  1998-10-03  0:00 ` Dmitriy Anisimkov
  1998-10-06  0:00 ` Robert I. Eachus
  2 siblings, 0 replies; 5+ messages in thread
From: Niklas Holsti @ 1998-10-03  0:00 UTC (permalink / raw)


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;




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

* Re: Generics and I/O
  1998-10-02  0:00 Generics and I/O William L Hayhurst
  1998-10-03  0:00 ` Niklas Holsti
  1998-10-03  0:00 ` Dmitriy Anisimkov
@ 1998-10-06  0:00 ` Robert I. Eachus
  1998-10-07  0:00   ` Dale Stanbrough
  2 siblings, 1 reply; 5+ messages in thread
From: Robert I. Eachus @ 1998-10-06  0:00 UTC (permalink / raw)



   Add the following:

   generic
       type Item is private;
--
       with procedure Put(I: in Item) is <>;
--
       package Foo is...

    Now if there is a visible Put procedure which matches the profile,
you are all set.  Otherwise you will have to pass it during the
instantiation explicity:

       with Ada.Integer_Text_IO;
       package Int_Foo is new Foo(Integer,Ada.Integer_Text_IO.Put);
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




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

* Re: Generics and I/O
  1998-10-06  0:00 ` Robert I. Eachus
@ 1998-10-07  0:00   ` Dale Stanbrough
  0 siblings, 0 replies; 5+ messages in thread
From: Dale Stanbrough @ 1998-10-07  0:00 UTC (permalink / raw)


Robert I. Eachus wrote:

"Now if there is a visible Put procedure which matches the profile,
 you are all set.  Otherwise you will have to pass it during the
 instantiation explicity:
 
        with Ada.Integer_Text_IO;
        package Int_Foo is new Foo(Integer,Ada.Integer_Text_IO.Put);"



But that won't work, because ada.integer_text_Io.put has a 2nd parameter,
the width field. If you want to do this (which i did recently), you have
to write a wrapper procedure which creates a bridge between the simple 
profile that the generic expects, and any parameters that the "real"
procedure has.

E.g.

   Generic expects procedure...

      procedure Put (Item : Element);

   Ada.Integer_Text_IO.Put has...

      procedure Put (Item : Integer; Width : Natural);


So you need a procedure...

   procedure Put (Item : Integer) is
   begin
      Ada.Integer_Text_IO.Put (Item, Ada.Integer_Text_IO.Default_Width);
   end;


Dale




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

end of thread, other threads:[~1998-10-07  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-10-02  0:00 Generics and I/O William L Hayhurst
1998-10-03  0:00 ` Niklas Holsti
1998-10-03  0:00 ` Dmitriy Anisimkov
1998-10-06  0:00 ` Robert I. Eachus
1998-10-07  0:00   ` Dale Stanbrough

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