comp.lang.ada
 help / color / mirror / Atom feed
* generic with procedure
@ 2004-09-28 21:52 Rick Santa-Cruz
  2004-09-28 23:56 ` Stephen Leake
  2004-09-29  2:11 ` Georg Bauhaus
  0 siblings, 2 replies; 5+ messages in thread
From: Rick Santa-Cruz @ 2004-09-28 21:52 UTC (permalink / raw)


Hi,

I am starting to read a bit about generic programming and I am a bit 
confused from it, cause it seems to me a bit different to 
template-programming in C++.
Sadly I can't figure out the difference between the following:
1.) generic
        type Element is private;
    package MyContainers is
        type MyContainer is private;
        procedure Some_Proc(Item : Element);
    end MyContainer;
2.) generic
        type Element is private;
    package MyContainers is
        type MyContainer is private;
     generic
        with procedure Some_Proc(Item : Element);
    end MyContainer;

I only have seen the "with procedure/function..." in the following sense:
generic
    type T is private;
    with procedure Some_Proc(Item : Element);
And I thought the sense of this is to tell the compiler that T has to 
declare the procedure Some_Proc, so that I can write in the body of my 
package something like:
....
Item1, Item2: Element

...
Item1.Some_Proc(Item2);

I sadly don't understand when to use the 2. case?

Thanks in advance,
Rick 





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

* Re: generic with procedure
  2004-09-28 21:52 generic with procedure Rick Santa-Cruz
@ 2004-09-28 23:56 ` Stephen Leake
  2004-09-29  0:06   ` Rick Santa-Cruz
  2004-09-29  2:11 ` Georg Bauhaus
  1 sibling, 1 reply; 5+ messages in thread
From: Stephen Leake @ 2004-09-28 23:56 UTC (permalink / raw)
  To: comp.lang.ada

"Rick Santa-Cruz" <rick_santa_cruz75@msn.com> writes:

> Hi,
> 
> I am starting to read a bit about generic programming and I am a bit 
> confused from it, cause it seems to me a bit different to 
> template-programming in C++.
> Sadly I can't figure out the difference between the following:
> 1.) generic
>         type Element is private;
>     package MyContainers is
>         type MyContainer is private;
>         procedure Some_Proc(Item : Element);
>     end MyContainer;

This is a complete package spec.

> 2.) generic
>         type Element is private;
>     package MyContainers is
>         type MyContainer is private;
>      generic
>         with procedure Some_Proc(Item : Element);
>     end MyContainer;

This is not legal syntax. You could mean:

2a.)
generic
   type Element is private;
package MyContainer_2a is
   type MyContainer is private;
   
   generic
      with procedure Some_Proc(Item : Element);
   procedure Foo;
end MyContainer_2a;

or 

2b.)
generic
   type Element is private;
package MyContainer_2b is
   type MyContainer is private;
   
   generic
   procedure Some_Proc(Item : Element);
   
end MyContainer_2b;


In 2a, 'Foo' is a generic procedure, that requires a generic formal
procedure parameter; presumably Foo calls Some_Proc in its body.

In 2b, 'Some_Proc' is a generic procedure, with no generic formal
parameters.

Here are some legal instantiations:

package Container_2a is new My_Container_2a (Integer);

procedure Proc (Item : Element) is 
    Put_Line ("hello 2a");
end Proc;

procedure Bar is new Container_2a.Foo (Proc);



package Container_2b is new My_Container_2b (Integer);

procedure Proc is new Container_2b.Some_Proc;

-- 
-- Stephe




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

* Re: generic with procedure
  2004-09-28 23:56 ` Stephen Leake
@ 2004-09-29  0:06   ` Rick Santa-Cruz
  2004-09-29 23:47     ` Stephen Leake
  0 siblings, 1 reply; 5+ messages in thread
From: Rick Santa-Cruz @ 2004-09-29  0:06 UTC (permalink / raw)


Hi,

>
> package Container_2a is new My_Container_2a (Integer);
>
> procedure Proc (Item : Element) is
>    Put_Line ("hello 2a");
> end Proc;
But this has to be part of a procedure, so that it will compile correctly? 
Is it correct that way:
procedure Tester is
    package Container_2a is new My_Container_2a (Integer);
    procedure Proc (Item : Element) is
        Put_Line ("hello 2a");
     end Proc;
     procedure Bar is new Container_2a.Foo (Proc);
     package Container_2b is new My_Container_2b (Integer);
     procedure Proc is new Container_2b.Some_Proc;
begin
    Bar(19);
end Tester;

Or are there still other ways? I read something with declare, but I am a 
real beginner in Ada.

Thanks in advance,
Rick





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

* Re: generic with procedure
  2004-09-28 21:52 generic with procedure Rick Santa-Cruz
  2004-09-28 23:56 ` Stephen Leake
@ 2004-09-29  2:11 ` Georg Bauhaus
  1 sibling, 0 replies; 5+ messages in thread
From: Georg Bauhaus @ 2004-09-29  2:11 UTC (permalink / raw)


Rick Santa-Cruz <rick_santa_cruz75@msn.com> wrote:
: Hi,
: 
: I am starting to read a bit about generic programming and I am a bit 
: confused from it, cause it seems to me a bit different to 
: template-programming in C++.
: Sadly I can't figure out the difference between the following:
: 1.) generic
:        type Element is private;
:    package MyContainers is
:        type MyContainer is private;
:        procedure Some_Proc(Item : Element);
:    end MyContainer;

Try starting from the simpler thing, a plain package, to see how
it is made generic, and what you need for making it generic.

package MyContainers is

   procedure Some_Proc (Item: Integer);

end MyContainers;

So now, Some_Proc works for Integer values. (You notice that it could as
well work with Characters, or Floats, or ...)
This is the point where you can make the package generic, by

  1) adding a formal parameter for the type that is to be used inside the
     package instead of just Integer

  2) substituting "Integer" with the generic type's name throughout the
     package.

generic
   with type Element is private;
package MyContainers is

   procedure Some_Proc (Item: Element); --  one substitution

end MyContainers;

When you create an instance of this generic package, you get a
normal package again, as if you had written one by hand.
(I think you have seen this in C++). The instance uses
the actual type that you have given in the instantiation. 
That is Some_Proc of that instance will work with the arguments
of the type that you have used for the instantiation.

package My_Int_Box is new MyContainers(Element => Integer);
package My_Char_Box is new MyContainers(Element => Character);

  My_Int_Box.Some_Proc(42);  -- OK
  My_Char_Box.Some_Proc(42); -- ERROR, 42 is not of type Character
  My_Int_Box.Some_Proc(4.2); -- ERROR, 4.2 is not of type Integer

package My_Float_Box is new MyContainers(Element => Float);

  My_Float_Box.Some_Proc(4.2); -- OK


The next thing is to ask why you have defined a _type_
        type MyContainer is private;
It is nowhere used in the above package. Some_Proc works
with Integer values, but not with MyContainer values.

Maybe you meant
  
   procedure Some_Proc (Cont: in out MyContainer; Item: Element);

wich does something with a MyContainer and with and Element,
for example, adding Item to Cont?

Notice that in Ada a package and a type are not the same.
In C++ there is some overlap of a package and a type when you
specify both as classes.

http://www.adapower.com   and
http://www.adaworld.com
link to useful material about these issues, in the learning sections.


-- Georg



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

* Re: generic with procedure
  2004-09-29  0:06   ` Rick Santa-Cruz
@ 2004-09-29 23:47     ` Stephen Leake
  0 siblings, 0 replies; 5+ messages in thread
From: Stephen Leake @ 2004-09-29 23:47 UTC (permalink / raw)
  To: comp.lang.ada

"Rick Santa-Cruz" <rick_santa_cruz75@msn.com> writes:

> But this has to be part of a procedure, so that it will compile
> correctly? 

Sorry. Here's a fully compileable example:

with Ada.Text_IO; use Ada.Text_IO;
procedure Rick
is
   generic
      type Element is private;
   package MyContainer_2a is
      generic
         with procedure Some_Proc(Item : Element);
      procedure Foo (Item : Element);
   end MyContainer_2a;

   package body MyContainer_2a is
      procedure Foo (Item : in Element)
      is begin
         Put_Line ("Foo 2a");
         Some_Proc (Item);
      end Foo;
   end MyContainer_2a;

   generic
      type Element is private;
   package MyContainer_2b is
      generic
      procedure Some_Proc(Item : Element);

   end MyContainer_2b;

   package body MyContainer_2b is
      procedure Some_Proc (Item : Element)
      is begin
         Put_Line ("Some_Proc 2b");
      end Some_Proc;

   end MyContainer_2b;


   package Container_2a is new MyContainer_2a (Integer);

   procedure Proc_2a (Item : Integer)
   is
      pragma Unreferenced (Item);
   begin
      Put_Line ("hello 2a");
   end Proc_2a;

   procedure Bar is new Container_2a.Foo (Proc_2a);

   package Container_2b is new MyContainer_2b (Integer);

   procedure Proc_2b is new Container_2b.Some_Proc;

begin

   Bar (1);
   Proc_2b (2);
end Rick;

-- 
-- Stephe




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

end of thread, other threads:[~2004-09-29 23:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-28 21:52 generic with procedure Rick Santa-Cruz
2004-09-28 23:56 ` Stephen Leake
2004-09-29  0:06   ` Rick Santa-Cruz
2004-09-29 23:47     ` Stephen Leake
2004-09-29  2:11 ` Georg Bauhaus

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