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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham 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!news2.google.com!news.glorb.com!newscon02.news.prodigy.net!prodigy.net!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: generic function and overloading Date: Thu, 18 Oct 2007 11:58:48 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <1192688972.967825.31130@t8g2000prg.googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls6.std.com 1192723129 5987 192.74.137.71 (18 Oct 2007 15:58:49 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 18 Oct 2007 15:58:49 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:3bLEUjcYsiumrzOuMN11J0LxG54= Xref: g2news2.google.com comp.lang.ada:2483 Date: 2007-10-18T11:58:48-04:00 List-Id: eliben writes: > Hello, > > 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; > > 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. Is > there any way I could combine the effects of generics and overloading > and get a single Bit_Set for all my types without writing the code N > times ? Others answered your main question. But do you really want to do arithmetic on these types, as well as setting/clearing bits? If all you want to do is set/clear bits, you might want to consider using a packed array of Boolean. Then to set the Nth bit you can say: X (N) := True; Alternatively, a packed array of type Bit, where: type Bit is range 0..1; If the individual bit numbers have some specific meaning, the index type could be an enumeration type. - Bob