comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve Doiel" <nospam_steved94@attbi.com>
Subject: Re: beginner ada generic fonction
Date: Sun, 24 Mar 2002 03:51:52 GMT
Date: 2002-03-24T03:51:52+00:00	[thread overview]
Message-ID: <s3cn8.110458$q2.11149@sccrnsc01> (raw)
In-Reply-To: 8235efb6.0203231910.65856099@posting.google.com

"annabelle" <ametayer98@hotmail.com> 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





  reply	other threads:[~2002-03-24  3:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-03-23  1:37 beginner ada generic fonction annabelle
2002-03-23  3:29 ` Steve Doiel
2002-03-24  2:55   ` annabelle
2002-03-24  3:10   ` annabelle
2002-03-24  3:51     ` Steve Doiel [this message]
2002-03-24 17:31       ` annabelle
2002-03-25  6:05         ` Simon Wright
2002-03-24 20:37       ` annabelle
replies disabled

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