comp.lang.ada
 help / color / mirror / Atom feed
From: "Beard, Frank" <beardf@spawar.navy.mil>
To: "'comp.lang.ada@ada.eu.org'" <comp.lang.ada@ada.eu.org>
Subject: RE: Generics???
Date: Mon, 2 Jul 2001 14:42:19 -0400
Date: 2001-07-02T14:42:19-04:00	[thread overview]
Message-ID: <mailman.994099384.29806.comp.lang.ada@ada.eu.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 793 bytes --]

You can do it as a private, but you have to pass in the operations.
Attached is an example of what I'm talking about.  Is this what
you're after?

Frank


-----Original Message-----
From: Michael Andersson [mailto:a98mican@ida.his.se]

Hi!
How do I specify that the generic parameter is a integer or a float. I
know that you can use 
type T is range <> to specify that T is any Integer-type and that you
can use type T is digit <> to specify that T is any type of float. But
how can I use both at the same time?
I've tried to use type T is private but then I can't use any of the
+,-,*,/ operators.

Need your help, please!
/Michael Andersson
_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada


[-- Attachment #2: GenericOperations.ads --]
[-- Type: application/octet-stream, Size: 802 bytes --]

generic
 
   type Item is private;
   
   with function "+" (the_Left, the_Right : Item) return Item;
   with function "-" (the_Left, the_Right : Item) return Item;
   with function "*" (the_Left, the_Right : Item) return Item;
   with function "/" (the_Left, the_Right : Item) return Item;
   
package GenericOperations is

   function Add (the_Left  : Item;
                 the_Right : Item) return Item;
                       
   function Subtract (the_Left  : Item;
                      the_Right : Item) return Item;
                       
   function Multiply (the_Left  : Item;
                      the_Right : Item) return Item;
                       
   function Divide (the_Left  : Item;
                    the_Right : Item) return Item;
                       
end GenericOperations;

[-- Attachment #3: GenericOperations.adb --]
[-- Type: application/octet-stream, Size: 835 bytes --]

package body GenericOperations is

 
   function Add (the_Left  : Item;
                 the_Right : Item) return Item is
   begin
      return the_Left + the_Right;
   end Add;

                          
   function Subtract (the_Left  : Item;
                      the_Right : Item) return Item is
   begin
      return the_Left - the_Right;
   end Subtract;
                       
                       
   function Multiply (the_Left  : Item;
                      the_Right : Item) return Item is
   begin
      return the_Left * the_Right;
   end Multiply;
                       
                       
   function Divide (the_Left  : Item;
                    the_Right : Item) return Item is
   begin
      return the_Left / the_Right;
   end Divide;
                       
                       
end GenericOperations;

[-- Attachment #4: GenericOperationsTest.ada --]
[-- Type: application/octet-stream, Size: 907 bytes --]

with Ada.Text_Io;
with GenericOperations;

procedure GenericOperationsTest is
 
   package FloatOperations is new
       GenericOperations(Item => float,
                         "+"  => "+",
                         "-"  => "-",
                         "*"  => "*",
                         "/"  => "/");

   x : float := 10.0;
   y : float :=  2.0;
   
   char : character := ' ';
   
begin
 
   Ada.Text_Io.New_Line(2);
   
   Ada.Text_Io.Put_Line("Add      => " & float'image(FloatOperations.Add(x,y)));
   Ada.Text_Io.Put_Line("Subtract => " & float'image(FloatOperations.Subtract(x,y)));
   Ada.Text_Io.Put_Line("Multiply => " & float'image(FloatOperations.Multiply(x,y)));
   Ada.Text_Io.Put_Line("Divide   => " & float'image(FloatOperations.Divide(x,y)));

   Ada.Text_Io.New_Line(2);
   
   Ada.Text_Io.Put("Hit any key ...");
   Ada.Text_Io.Get_Immediate(char);
      
end GenericOperationsTest;

             reply	other threads:[~2001-07-02 18:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-02 18:42 Beard, Frank [this message]
2001-07-03 10:32 ` Generics??? Anders Wirzenius
2001-07-03 17:31   ` Generics??? Stephen Leake
2001-07-03 19:09   ` Generics??? tmoran
2001-07-04  5:30     ` Generics??? Anders Wirzenius
  -- strict thread matches above, loose matches on Subject: below --
2001-07-02 22:33 Generics??? Michael Andersson
2001-07-02 17:48 ` Generics??? James Rogers
2001-07-02 18:54   ` Generics??? Ehud Lamm
1992-09-14 15:00 Generics? cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!jvnc.net!net
1992-09-09  5:26 Generics? Andrew Dunstan
1992-09-09  1:34 Generics?  Rob Allen
replies disabled

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