comp.lang.ada
 help / color / mirror / Atom feed
From: Stefan Lucks <lucks@th.informatik.uni-mannheim.de>
Subject: Re: generic function and overloading
Date: Thu, 18 Oct 2007 13:30:05 +0200
Date: 2007-10-18T13:30:05+02:00	[thread overview]
Message-ID: <Pine.LNX.4.64.0710181327030.9652@th.informatik.uni-mannheim.de> (raw)
In-Reply-To: <1192688972.967825.31130@t8g2000prg.googlegroups.com>

> I have a few "hardware" types, for example uint16 and uint32. And I
> want to write a functions that will set or clear bits of such types.

> [...] I can define a generic Bit_Set, but then I have to
> specialize it for uin16 and uint32 with different function names.

I don't think so, see the example below:


--begin example
with Ada.Text_IO;

procedure X is

    generic
       type Swap_Type is private;
    procedure Gen_Swap (X,Y: in out Swap_Type);

    procedure Gen_Swap (X,Y: in out Swap_Type) is
       Tmp: Swap_Type:= X;
    begin
       X := Y;
       Y := Tmp;
    end Gen_Swap;

    type U_Int_8  is mod 2**8;
    type U_Int_16 is mod 2**16;
    type U_Int_32 is mod 2**32;

    procedure Swap is new Gen_Swap(U_Int_8);
    procedure Swap is new Gen_Swap(U_Int_16);
    procedure Swap is new Gen_Swap(U_Int_32);

    I,J: U_Int_8  := 255;
    S,T: U_Int_16 := 255;

begin
      J := J + 1;
      T := T + 1;
      Swap(I,J);
      Swap(S,T);
      if I >= J then
         Ada.Text_IO.Put_Line("Error for U_Int_8.");
      elsif S <= T then
         Ada.Text_IO.Put_Line("Error for U_Int_16.");
      else
         Ada.Text_IO.Put_Line("Everything fine, as expected!");
      end if;
end X;

-- end example

Now, if you change "Gen_Swap" into "Swap", you get an error message. 
Perhaps you got a similar error message and was puzzled by this.

So long

Stefan

-- 
Stefan Lucks      (moved to Bauhaus-University Weimar, Germany)
 		       <Stefan.Lucks at medien.uni-weimar.de>
------  I  love  the  taste  of  Cryptanalysis  in  the  morning!  ------





  parent reply	other threads:[~2007-10-18 11:30 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-18  7:28 generic function and overloading eliben
2007-10-18  8:18 ` Jean-Pierre Rosen
2007-10-18  9:31 ` Dmitry A. Kazakov
2007-10-18  9:33   ` Dmitry A. Kazakov
2007-10-18 10:32 ` Georg Bauhaus
2007-10-18 11:07 ` Stephen Leake
2007-10-18 11:30 ` Stefan Lucks [this message]
2007-10-18 15:58 ` Robert A Duff
2007-10-18 20:20   ` Simon Wright
2007-10-18 21:23     ` Robert A Duff
2007-10-19  5:19       ` Simon Wright
2007-10-19 13:59         ` Robert A Duff
2007-10-18 18:58 ` Dr. Adrian Wrigley
2007-10-18 22:30   ` Adam Beneschan
replies disabled

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