comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Generic in out object parameters
Date: 1997/04/18
Date: 1997-04-18T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680001804972316130001@news.ni.net> (raw)
In-Reply-To: Forum.861207636.14373.petera@woodlands



In article <Forum.861207636.14373.petera@woodlands>, petera@ece.uc.edu wrote:

>A generic unit may have an generic object parameter of mode in out.  I
understand that this acts as a renaming of the actual parameter supplied
when the unit is instantiated.
>
>Can someone give me a good motivating example of why this is useful?
>
>I'm not looking for an example that illustrates the mechanism - I
understand that.  I'm looking for a real-world example that illustrates how
the mechanism is useful.

Use it when you want to bind to an already-existing (possibly global)
variable.  I wrote a utility to work with an instantiation of
Text_IO.Integer_IO, to augment the behavior of Put:

generic
   type Num is range <>;
   with procedure Put (Item : Num; Width : Field; Base : Number_Base);
   Default_Width : in out Field;
procedure Generic_Put (Item  : Num;
                                           Width : Field := Default_Width);

So the client (me) could use his instantiation of Text_IO.Integer_IO.   I
needed to get at Default_Width, which is a global variable in the spec of
Text_IO.Integer_IO, so I just bound the instantiation to it:

procedure Put is new Generic_Put (T, TIO.Put, TIO.Default_Width);

Of course, with Ada 95 I can just pass in the instantiation of the package
as a generic actual, so I wouldn't need to do as I've shown above.

The idea is that you can bind the package to some global data - usually to
read - because the allocation of that data is outside the control of the
instantiator.

In David Watt's book, Ada: Language and Methodology, he gives the example
of a package that binds to a device control block:

generic
   Control_Block : in out Device_Data;
package VDU is  ...;

In his example, the generic VDU utilities operate on (already-existing)
device data, but independent of any particular device data object.

Honestly, this is a seldom used feature of Ada.  Especially nowadays,
because we can bind objects to other objects via access discriminants,
binding a package to an object seems a bit out of fashion.

Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




      parent reply	other threads:[~1997-04-18  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-04-16  0:00 Generic in out object parameters petera
1997-04-17  0:00 ` Stephen Leake
1997-04-19  0:00   ` Robert Dewar
1997-04-18  0:00 ` Matthew Heaney [this message]
replies disabled

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