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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,31a8e118c303e8b3 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-03-23 19:51:53 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!feed2.news.rcn.net!rcn!wn14eed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!sccrnsc01.POSTED!not-for-mail From: "Steve Doiel" Newsgroups: comp.lang.ada References: <8235efb6.0203221737.44abefff@posting.google.com> <3ESm8.101615$q2.10433@sccrnsc01> <8235efb6.0203231910.65856099@posting.google.com> Subject: Re: beginner ada generic fonction X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 Message-ID: NNTP-Posting-Host: 12.225.227.101 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc01 1016941912 12.225.227.101 (Sun, 24 Mar 2002 03:51:52 GMT) NNTP-Posting-Date: Sun, 24 Mar 2002 03:51:52 GMT Organization: AT&T Broadband Date: Sun, 24 Mar 2002 03:51:52 GMT Xref: archiver1.google.com comp.lang.ada:21602 Date: 2002-03-24T03:51:52+00:00 List-Id: "annabelle" wrote in message news:8235efb6.0203231910.65856099@posting.google.com... > Haaa ! I finally got it ! Yes, it seems I can just cut and paste ONE > function and use it with different parameters... I just discovered the > real meaning of genericity. I'm so happy. Since your previous post referred to "cut and paste" of code, I'm not sure you do understand genericity (or at least the use of generics). Here is an example (one that is often used). Suppose you have a function that returns maximum of two integers: function Max( a, b : integer ) return integer is begin if a > b then return a; else return b; end if; end Max; Now suppose you want a function that returns the maximum of two float values. If you make a copy of the "Max" function shown and subtitute the word "float" for "integer" you'll have just such a function. Later you decide you want a "Max" function for "Long_Float". Again you could do a cut and paste, and fix up the copy. But... doing a cut and paste and fixing up the copy is error prone (not to mention tedious). What would really be nice is a way to automate the process... enter "generics". With generics I create a definition of the procedure where I use placeholders for the actual types and functions I want to use inside my function. For the max function I use "dataType" as place holder for the type of data I'll use for my maximum function. I also chose to make a placeholder for the greater than operation to make the function even more general (that way it can be used with composite or simple types). -- Here's the part that defines the parameters to the template and a prototype -- of the function. generic type dataType is private; with function ">"( left, right : dataType ) return boolean; function Gen_Max( a, b : dataType ) return dataType; -- Here's the generic code using "dataType" and ">" defined from above. function Gen_max( a, b : dataType ) return dataType is begin if a > b then return a; else return b; end if; end Gen_Max; Now that I have this generic definition, to create a Max function for integers I use: function Max is new Gen_Max( integer, ">" ); If I want a Max function for float's I use: function Max is new Gen_Max( float, ">" ); For long floats I use: function Max is new Gen_Max( long_float, ">" ); I hope this helps, SteveD > thanks again. > > Annabelle