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,c08a7609345f4e5 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!i13g2000yqd.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Limited use for limited with? Date: Tue, 28 Sep 2010 02:18:05 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <853314bc-0f79-435f-86a5-d7bcdd610731@c10g2000yqh.googlegroups.com> NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1285665515 8974 127.0.0.1 (28 Sep 2010 09:18:35 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 28 Sep 2010 09:18:35 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: i13g2000yqd.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:14287 Date: 2010-09-28T02:18:05-07:00 List-Id: Maciej Sobczak wrote on comp.lang.ada: > I have found a problem with the intended application of limited with. > > Consider a package specifying some object-oriented construct: > > package Objects is > > =A0 =A0type Object is interface; > =A0 =A0type Object_Access is access all Object'Class; > > =A0 =A0procedure Do_Something (X : in out Object) is abstract; > > end Objects; > > In such cases I routinely define the XYZ_Access type and later use it > wherever pointers to class-wide XYZ are needed. > > Now consider a package that uses the above in a limited way (pun > intended), where only pointers to class-wide type are needed: > > with Objects; > package Object_Users is > > =A0 =A0 procedure Use_Object (X : Objects.Object_Access); > > end Object_Users; > > The problem is that is some cases it would be more convenient (or just > more self-documenting from the design perspective) to do limited with > instead, but unfortunately this makes Object_Access unavailable. It is > OK to use anonymous access type, at least in some cases like here: > > =A0 =A0 procedure Use_Object (X : access Objects.Object'Class); > > but I find that uncomfortable - after all, the proper access type is > already defined for exactly this purpose. > > In such cases, where the design intent is pretty clear (pass around > references to Objects) I find that limited with does not really bring > the functionality that it is supposed to provide. In some more > involving cases I was forced to introduce additional and completely > artificial packages, where limited with would be a perfect fit. > > Any thoughts on this? I generally think twice or three times before declaring an access type in the same package as the object type. In fact, I think twice before declaring any access type at all :) To me, an access type makes the package unclean. Since, in Ada, all objects of tagged types are passed by reference and since Ada has class-wide types, you do not need any access type to achieve pass-by-reference semantics or dynamic dispatching. This leaves dynamic memory allocation as the only remaining justification for access types. If I need dynamic memory allocation, I'd consider using a container (possibly indefinite) before introducing an access type. By this reasoning, a general (!) class-wide access type to an interface (as opposed to a tagged type) may not be justified as you're never going to dynamically allocate interfaces, only tagged types that implement the interface. If, after all these musings, I decide that an access type is really necessary after all (perhaps because I'm writing my own container), then I usually declare it where I need the access type, i.e. in another unit, possibly even in the body. Like so: package Objects is type Object is interface; procedure Do_Something (X : in out Object) is abstract; end Objects; with Objects; procedure Client is type Object_Access is access Objects.Object'Class; -- not "access all" O : Object_Access :=3D new Objects.Object; begin ... end Client; Taking this approach further, I can declare the access type in a nested scope. When the access type goes out of scope, all objects allocated through it are deallocated. This is a form of automatic garbage collection. If you take this approach, your "limited with" problem will disappear. As an extra bonus, you can make your package Objects Preelaborated, or even Pure. -- Ludovic Brenta.