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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC 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!news4.google.com!feeder3.cambrium.nl!feed.tweaknews.nl!newsfeed.kamp.net!newsfeed.kamp.net!213.239.142.2.MISMATCH!feed.xsnews.nl!border-1.ams.xsnews.nl!news.netcologne.de!newsfeed-fusi2.netcologne.de!newsfeed01.chello.at!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Self pointer in limited record Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1183577468.034566.57830@n60g2000hse.googlegroups.com> Date: Thu, 5 Jul 2007 10:22:59 +0200 Message-ID: NNTP-Posting-Date: 05 Jul 2007 10:19:46 CEST NNTP-Posting-Host: fff09f58.newsspool1.arcor-online.net X-Trace: DXC=kFlS=ae^c2TPU8j_I0DN6_ic==]BZ:af^4Fo<]lROoRQFl8W>\BH3YRCnFnl9_K7B]DNcfSJ;bb[UFCTGGVUmh?TLK[5LiR>kgRF>W0IGl;o<[ X-Complaints-To: usenet-abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:16417 Date: 2007-07-05T10:19:46+02:00 List-Id: On Wed, 04 Jul 2007 12:31:08 -0700, Maciej Sobczak wrote: > Consider: > > type T_Access is access all T; > type T is new Ada.Finalization.Limited_Controlled with record > Self : T_Access := T'Unchecked_Access; > -- more components > -- ... > end record; > > I have seen this pattern repeatedly. What is the use for Self? The pattern (also called Rosen trick) is used: 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 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 Bar (T'Class (X)); -- This dispatches as well The later (view conversion) should better be removed from the language, so I always prefer Rosen trick for such purpose. Both defeat the type system in some sense and potentially indicate a design problem. > And why is it Unchecked? Because of accessibility rules. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de