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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f4e1ae22e5bbdd9f X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Generic in out object parameters Date: 1997/04/18 Message-ID: #1/1 X-Deja-AN: 235902122 References: Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-04-18T00:00:00+00:00 List-Id: In article , 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 (818) 985-1271