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,5c89acd494ea9116 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.teledata-fn.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Sat, 01 Sep 2007 15:33:09 +0200 From: Georg Bauhaus Organization: # User-Agent: Thunderbird 2.0.0.6 (Macintosh/20070728) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Self pointer in limited record References: <1183577468.034566.57830@n60g2000hse.googlegroups.com> <1188578849.187422.280620@50g2000hsm.googlegroups.com> <9fy1xoukz1e3$.h574sqmiauri$.dlg@40tude.net> In-Reply-To: <9fy1xoukz1e3$.h574sqmiauri$.dlg@40tude.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <46d968ee$0$30368$9b4e6d93@newsspool4.arcor-online.net> NNTP-Posting-Date: 01 Sep 2007 15:28:14 CEST NNTP-Posting-Host: 0557cbd7.newsspool4.arcor-online.net X-Trace: DXC=90AZ2jW=;7Sf8j24CD<3lP4IUK Dmitry A. Kazakov wrote: > On Fri, 31 Aug 2007 16:47:29 -0000, amado.alves@gmail.com wrote: > >> Then you have seen illegal Ada code repeatedly. I wish this were >> possible myself. Or more simply: >> >> type T is -- limited or not >> Self : access T := T'Unchecked_Access; >> ... >> end; > > T cannot be non-limited, because otherwise passing it by copy would make > rubbish out of Self. In any case it would make little sense if not access > T'Class. I think there is an interesting use of a .Self pointer of simple limited records with state variables of packages. Say a procedure in a package P controls the state of some variable in the package's body. The state variable is of type access T, where T is a limited record type declared in P. The purpose of the state variable is to remember the object of type T that will be the target of subsequent package operations. A natural way to remember a particular object is to point to the object. But package clients should not have to worry about this pointing mechanism. So the package shouldn't declare a public access type used for internal mechanism only. Instead there is a procedure of one T argument that can be called by clients when they want to indicate the object to be used in subsequent P operations. This is where .Self can be used. We cannot take 'Access or 'Unchecked_Access of subprogram arguments unless they are aliased (such as those of type T'Class). But the .Self component of the subprogram's argument supplies the access value that is needed for storing a pointer to the argument in the package body. package P is type T is limited private; procedure Choose (Selected: in out T); -- sets target T for subsequent operations procedure Work; private type Bits is array(0 .. 15) of Boolean; type T_Access is access all T; type T is limited record Self: T_Access := T'unchecked_access; Slots: Bits; end record; end P; package body P is Current: T_Access; -- state variable, target object procedure Choose (Selected: in out T) is begin Current := Selected.Self; -- here end Choose; procedure Work is begin Current.Slots(3) := not Current.Slots(3); end Work; end P; (The true O-O programmer might suggest that we should simply pass an additional object-as-module parameter to every package subprogram... )