comp.lang.ada
 help / color / mirror / Atom feed
* beginner ada generic fonction
@ 2002-03-23  1:37 annabelle
  2002-03-23  3:29 ` Steve Doiel
  0 siblings, 1 reply; 8+ messages in thread
From: annabelle @ 2002-03-23  1:37 UTC (permalink / raw)


Hello,

I was given a spec(ADS)using genericity. I have to use the function
"TheKey" (see below)

______________________________________________________________________
GENERIC
  TYPE Element_type IS Private
  Key_type IS Private;                              

  WITH Function TheKey (my_item : Element_type) Return Key_type; 

  WITH Function "<"  (Left,Right: Key_type) Return Boolean; 
______________________________________________________________________

I am trying to use the function "TheKey" in my ADA program but I need
to use the function for TWO different type of elements !!!
______________________________________________________________________

	function TheKey(My_item: a_course) return Course_key_type is
		
	begin
		Return My_item.course_key;
	end lacle;
______________________________________________________________________

How can I use the same function for another type (a_student)??

Please HELP !!!



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

* Re: beginner ada generic fonction
  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
  0 siblings, 2 replies; 8+ messages in thread
From: Steve Doiel @ 2002-03-23  3:29 UTC (permalink / raw)


Since you obviously don't have a textbook, I'd like to point you to the
relevent chapter of one you can find on-line:

http://www.it.bton.ac.uk/staff/je/adacraft/ch12.htm

I hope this helps,
SteveD


"annabelle" <ametayer98@hotmail.com> wrote in message
news:8235efb6.0203221737.44abefff@posting.google.com...
> Hello,
>
> I was given a spec(ADS)using genericity. I have to use the function
> "TheKey" (see below)
>
> ______________________________________________________________________
> GENERIC
>   TYPE Element_type IS Private
>   Key_type IS Private;
>
>   WITH Function TheKey (my_item : Element_type) Return Key_type;
>
>   WITH Function "<"  (Left,Right: Key_type) Return Boolean;
> ______________________________________________________________________
>
> I am trying to use the function "TheKey" in my ADA program but I need
> to use the function for TWO different type of elements !!!
> ______________________________________________________________________
>
> function TheKey(My_item: a_course) return Course_key_type is
>
> begin
> Return My_item.course_key;
> end lacle;
> ______________________________________________________________________
>
> How can I use the same function for another type (a_student)??
>
> Please HELP !!!





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

* Re: beginner ada generic fonction
  2002-03-23  3:29 ` Steve Doiel
@ 2002-03-24  2:55   ` annabelle
  2002-03-24  3:10   ` annabelle
  1 sibling, 0 replies; 8+ messages in thread
From: annabelle @ 2002-03-24  2:55 UTC (permalink / raw)


Thank you very much for your answer.

But I still dont get how I can make this function re-usable :

  	-------------------------------------------------------------------
  	function laCle(Mon_item : T_inscription) return t_code_Cours is
		
	begin
		Return Mon_item.Code_cours;
  	end lacle;
  	-------------------------------------------------------------------

I tried to cut'n paste the function to reuse it with another type of element :

  	-------------------------------------------------------------------
  	function laCle(Mon_item : T_Course) return t_code_Cours is
		
	begin
		Return Mon_item.Code_course;
  	end lacle;
  	-------------------------------------------------------------------

But it does not work...



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

* Re: beginner ada generic fonction
  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
  1 sibling, 1 reply; 8+ messages in thread
From: annabelle @ 2002-03-24  3:10 UTC (permalink / raw)


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.

thanks again.

Annabelle



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

* Re: beginner ada generic fonction
  2002-03-24  3:10   ` annabelle
@ 2002-03-24  3:51     ` Steve Doiel
  2002-03-24 17:31       ` annabelle
  2002-03-24 20:37       ` annabelle
  0 siblings, 2 replies; 8+ messages in thread
From: Steve Doiel @ 2002-03-24  3:51 UTC (permalink / raw)


"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





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

* Re: beginner ada generic fonction
  2002-03-24  3:51     ` Steve Doiel
@ 2002-03-24 17:31       ` annabelle
  2002-03-25  6:05         ` Simon Wright
  2002-03-24 20:37       ` annabelle
  1 sibling, 1 reply; 8+ messages in thread
From: annabelle @ 2002-03-24 17:31 UTC (permalink / raw)


Thank you for your efforts ! Your explanation was helpful...

The thing is, I was given the following spec and I cant change it.
I've GOT to use it as it is.

I can make the "LaCle" function to work but now I have trouble with
the "<" function...

I would love to have an example of how I call this "<" in my ADB file
and how do  I define this function in my ADA project...

I know I am a pain in the neck !!! I have great difficulty with the
concept but I know I CAN understand eventually !!!

GENERIC
  TYPE Type_Element IS Private;                               -- Type
de l'enregistrement a gerer
  TYPE Type_Cle IS Private;                                   -- Type
de la cle de l'enregistrement
  WITH Function LaCle (mon_item : Type_Element) Return Type_Cle;  --
Fonction qui retourne la valeur de la cle
  WITH Function "<"  (Gauche,Droite : Type_Cle) Return Boolean;  --
Importation de l'operateur < pour comparer des cles

I AM SO MISERABLE !!!

Annabelle M�tayer



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

* Re: beginner ada generic fonction
  2002-03-24  3:51     ` Steve Doiel
  2002-03-24 17:31       ` annabelle
@ 2002-03-24 20:37       ` annabelle
  1 sibling, 0 replies; 8+ messages in thread
From: annabelle @ 2002-03-24 20:37 UTC (permalink / raw)


I got it to work, finally. It was working all the time it's just my
TESTS that were flawed and it did not seem to work.

Tank you very much, SteveD, for taking the time to answer my very
confuse questions !!!

And I will try to use your suggestions for instanciation instead of
cut'n paste !

thank you

Annabelle M�tayer



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

* Re: beginner ada generic fonction
  2002-03-24 17:31       ` annabelle
@ 2002-03-25  6:05         ` Simon Wright
  0 siblings, 0 replies; 8+ messages in thread
From: Simon Wright @ 2002-03-25  6:05 UTC (permalink / raw)


ametayer98@hotmail.com (annabelle) writes:

> The thing is, I was given the following spec and I cant change it.
> I've GOT to use it as it is.
> 
> I can make the "LaCle" function to work but now I have trouble with
> the "<" function...
> 
> I would love to have an example of how I call this "<" in my ADB file
> and how do  I define this function in my ADA project...
> 
> I know I am a pain in the neck !!! I have great difficulty with the
> concept but I know I CAN understand eventually !!!
> 
> GENERIC
>   TYPE Type_Element IS Private;                               -- Type
> de l'enregistrement a gerer
>   TYPE Type_Cle IS Private;                                   -- Type
> de la cle de l'enregistrement
>   WITH Function LaCle (mon_item : Type_Element) Return Type_Cle;  --
> Fonction qui retourne la valeur de la cle
>   WITH Function "<"  (Gauche,Droite : Type_Cle) Return Boolean;  --
> Importation de l'operateur < pour comparer des cles

Removing the comments so that it fits on a line,

GENERIC
  TYPE Type_Element IS Private;
  TYPE Type_Cle IS Private;
  WITH Function LaCle (mon_item : Type_Element) Return Type_Cle;
  WITH Function "<"  (Gauche,Droite : Type_Cle) Return Boolean;

there is something missing, namely what it is that is is to be generic.

The next line must start with PACKAGE, PROCEDURE or FUNCTION.

The idea is that *you* supply a function LaCle and a function "<"
which work as indicated with a Type_Cle (whatever that is), and then
the *instantiation of the generic* calls the functions to do what it
needs to do ...



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

end of thread, other threads:[~2002-03-25  6:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2002-03-24 17:31       ` annabelle
2002-03-25  6:05         ` Simon Wright
2002-03-24 20:37       ` annabelle

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