From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,d7b3e24220f64b53 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!newsfeed.stueberl.de!news-mue1.dfn.de!news-ham1.dfn.de!news.uni-hamburg.de!cs.tu-berlin.de!uni-duisburg.de!not-for-mail From: Georg Bauhaus Newsgroups: comp.lang.ada Subject: Re: generic with procedure Date: Wed, 29 Sep 2004 02:11:58 +0000 (UTC) Organization: GMUGHDU Message-ID: References: NNTP-Posting-Host: l1-hrz.uni-duisburg.de X-Trace: a1-hrz.uni-duisburg.de 1096423918 11076 134.91.1.34 (29 Sep 2004 02:11:58 GMT) X-Complaints-To: usenet@news.uni-duisburg.de NNTP-Posting-Date: Wed, 29 Sep 2004 02:11:58 +0000 (UTC) User-Agent: tin/1.5.8-20010221 ("Blue Water") (UNIX) (HP-UX/B.11.00 (9000/800)) Xref: g2news1.google.com comp.lang.ada:4352 Date: 2004-09-29T02:11:58+00:00 List-Id: Rick Santa-Cruz 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