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.4 required=5.0 tests=AC_FROM_MANY_DOTS,BAYES_00 autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b5ab7c96b188b59e X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-01-16 05:56:54 PST Path: archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!crtntx1-snh1.gtei.net!news.gtei.net!newsfeed1.easynews.com!easynews.com!easynews!cyclone1.gnilink.net!spamkiller2.gnilink.net!nwrdny03.gnilink.net.POSTED!0f19ed38!not-for-mail From: "Frank J. Lhota" Newsgroups: comp.lang.ada References: <4003EEEC.40106@noplace.com> <%1WMb.25$3f4.77748@news20.bellglobal.com> Subject: Re: The "()" operator revisited. X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Message-ID: Date: Fri, 16 Jan 2004 13:56:53 GMT NNTP-Posting-Host: 151.203.221.201 X-Complaints-To: abuse@verizon.net X-Trace: nwrdny03.gnilink.net 1074261413 151.203.221.201 (Fri, 16 Jan 2004 08:56:53 EST) NNTP-Posting-Date: Fri, 16 Jan 2004 08:56:53 EST Xref: archiver1.google.com comp.lang.ada:4462 Date: 2004-01-16T13:56:53+00:00 List-Id: "Robert A Duff" wrote in message news:wcczncpgjzv.fsf@shell01.TheWorld.com... > The C++ way has some advantages over the Ada way (where the user's > control is split between Finalize and Adjust) because it's sometimes > convenient to get your hands on both sides of the assignment at the same > time. You can get access to both sides of an assignment in Ada 95, using a variant of the Rosen trick. The idea is to add a component to the controlled object that holds an access value for the object itself, e.g. type My_Controlled_Access is access all My_Controlled_Type; type My_Controlled_Type is new Ada.Finalization.Controlled with record Self : My_Controlled_Access; -- Always accesses the containing object. ... end record; procedure Initialize(Object : in out My_Controlled_Type) is begin Object.Self := Object'Access; ... end Initialize; Now when Adjust is called, the object will be a bit copy of the right hand side of the assignment. In particular, this gives you a component with an access value for the right hand side. In Adjust, we can simply save the value of this component, and reset it to access the left hand of the assignment (i.e. the current object). Now we have access to both sides of the assignment, e.g. procedure Adjust(Object : in out My_Controlled_Type) is Right_Hand_Side : constant My_Controlled_Access := Object.Self; begin Object.Self := Object'Access; -- Now the left hand side of the assignment is in Object, -- and the Right Hand side is in the saved value of Object.Self. ... end Adjust; Note that although the Rosen trick works, it is, after all, still a trick. I agree that there are probably cases where it would be nice to be able to define the ":=" operator in Ada. As someone who has coded in both Ada and C++, I must however admit that in most cases I have found the Initialize / Adjust / Finalize coding to be easier than doing the C++ equivalents.