comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <sb463ba@l1-hrz.uni-duisburg.de>
Subject: Re: generic with procedure
Date: Wed, 29 Sep 2004 02:11:58 +0000 (UTC)
Date: 2004-09-29T02:11:58+00:00	[thread overview]
Message-ID: <cjd5le$aq4$1@a1-hrz.uni-duisburg.de> (raw)
In-Reply-To: cjcmeu$ljq$03$1@news.t-online.com

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



      parent reply	other threads:[~2004-09-29  2:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]
replies disabled

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