comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.tsoh+bauhaus@maps.futureapps.de>
Subject: Re: generic function and overloading
Date: Thu, 18 Oct 2007 12:32:15 +0200
Date: 2007-10-18T12:32:21+02:00	[thread overview]
Message-ID: <47173635$0$30380$9b4e6d93@newsspool4.arcor-online.net> (raw)
In-Reply-To: <1192688972.967825.31130@t8g2000prg.googlegroups.com>

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?)



  parent reply	other threads:[~2007-10-18 10:32 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 [this message]
2007-10-18 11:07 ` Stephen Leake
2007-10-18 11:30 ` Stefan Lucks
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