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!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Thu, 18 Oct 2007 12:32:15 +0200 From: Georg Bauhaus User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: generic function and overloading References: <1192688972.967825.31130@t8g2000prg.googlegroups.com> In-Reply-To: <1192688972.967825.31130@t8g2000prg.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-8-I; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <47173635$0$30380$9b4e6d93@newsspool4.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 18 Oct 2007 12:32:21 CEST NNTP-Posting-Host: 34b3ce8f.newsspool4.arcor-online.net X-Trace: DXC=SW<_hT70maF2jYf>V4L0gL4IUK eliben wrote: > 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. Implicit instantiation might not be in line with the Ada way of asking the programmers to actually say what they are doing on behalf of readers, reviewers, and themselves. (Otherwise use polymorphism and overriding.) Extending J-P. Rosen's hint, you can employ visibility control and even add a helpful prefix to the same instance name Bit_Set instantiated in different locations (or renamed there), e.g. Send (Lawnmower, Gardening_Device_Interface.Bit_Set(Motor_Control, Silent_Mode)); Imagine a Hard_Drive_Interface if Gardening_Device_Interface seems unreal. (It also wouldn't have to be a Lawnmower receiving the bits, I guess, or do these things meanwhile have hard drives?)