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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,5c89acd494ea9116 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!n2g2000hse.googlegroups.com!not-for-mail From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Re: Self pointer in limited record Date: Thu, 05 Jul 2007 03:35:54 -0700 Organization: http://groups.google.com Message-ID: <1183631754.917400.257350@n2g2000hse.googlegroups.com> References: <1183577468.034566.57830@n60g2000hse.googlegroups.com> NNTP-Posting-Host: 137.138.37.241 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1183631755 4329 127.0.0.1 (5 Jul 2007 10:35:55 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 5 Jul 2007 10:35:55 +0000 (UTC) In-Reply-To: User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070601 Red Hat/1.5.0.12-0.1.slc3 Firefox/1.5.0.12,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: n2g2000hse.googlegroups.com; posting-host=137.138.37.241; posting-account=Ch8E9Q0AAAA7lJxCsphg7hBNIsMsP4AE Xref: g2news1.google.com comp.lang.ada:16418 Date: 2007-07-05T03:35:54-07:00 List-Id: On 5 Lip, 10:22, "Dmitry A. Kazakov" wrote: > > What is the use for Self? > > The pattern (also called Rosen trick) is used: That's cheating - I was told there are no tricks in Ada! ;-) > 1. to having mutable arguments of functions: > > function Search (X : T; K : Key) return Value is > Object : T renames X.Self.all; > begin > ... -- Modify the search cache associated with X via > -- mutable Object view OK, makes sense. I have seen it used with rendezvous and I guess the motivation was similar. > 2. to re-dispatch from primitive operations: > > type T_Access is access all T'Class; -- Note 'Class > type T is new Ada.Finalization.Limited_Controlled with record > Self : T_Access := T'Unchecked_Access; > > procedure Bar (X : T); -- Primitive > procedure Foo (X : T); -- Primitive > > procedure Foo (X : T) is > Object : T'Class renames X.Self.all; > begin > Bar (X); -- This does not dispatch! > Bar (Object.Self.all); -- This dispatches I guess Bar (Object) should be enough? Yes, it makes sense as well. > Both defeat the type system in some sense and potentially indicate a > design problem. I have seen it applied *regularly* in some Ada project, so it looked like "default" idiom for OO programming. > > And why is it Unchecked? > > Because of accessibility rules. You mean - the access type is defined in the same scope as the record and there is a potential danger that the access component in the record can point to some other record that may disappear leading to dangling pointer? Thank you for explanation. -- Maciej Sobczak http://www.msobczak.com/