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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,1514d4f994aed7aa X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!news.glorb.com!news.cs.univ-paris8.fr!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!npeer.de.kpn-eurorings.net!npeer1.kpn.DE!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: generic function and overloading Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1192688972.967825.31130@t8g2000prg.googlegroups.com> Date: Thu, 18 Oct 2007 11:31:15 +0200 Message-ID: <14bfr1ubkvtjt.1rm8b553g3fke.dlg@40tude.net> NNTP-Posting-Date: 18 Oct 2007 11:25:00 CEST NNTP-Posting-Host: 3a2fe8fa.newsspool4.arcor-online.net X-Trace: DXC=?l3@K[`iN2>d8Nb@@ZG@b=4IUKkg2^Xi@dRkbaB1 X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:2478 Date: 2007-10-18T11:25:00+02:00 List-Id: On 18 Oct 2007 00:28:38 -0700, eliben wrote: > 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. > For example: > > function Bit_Set(word: uint16; bitn: natural) return uint16; > > function Bit_Set(word: uint16; bitn: natural) return uint16 is > mask: uint16 := 2**bitn; > begin > return word or mask; > end Bit_Set; Bit_Set (X, Natural'Last)? > However, I realize that such a function would be almost completely > duplicated for the uint32 type. In C++ I would probably define it as a > template on the type of the word, and the compiler would do the job > for me. In Ada, however, when using generics it doesn't seem I can > leave the same function name for all types (as I would do in > overloading). I can define a generic Bit_Set, but then I have to > specialize it for uin16 and uint32 with different function names. You can overload generic instances in exactly same way as you would other subprograms: generic type Data is mod <>; type Bit_No is range <>; function Generic_Set_Bit (Word : Data; No : Bit_No) return Data; function Generic_Set_Bit (Word : Data; No : Bit_No) return Data is pragma Assert (Data'Last = 2 ** Integer (Bit_No'Last + 1) - 1); begin return Word or 2 ** Integer (No); end Generic_Set_Bit; subtype Word_Index is Natural range 0..15; function Set_Bit is new Generic_Set_Bit (Unsigned_16, Word_Index); subtype Double_Word_Index is Natural range 0..31; function Set_Bit is new Generic_Set_Bit (Unsigned_32, Double_Word_Index); -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de