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,bf564bb3bcde0817 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Renaming versus initialisation Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.14.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: <1qys7gc3aqky9$.1qtzg7epyh6kk$.dlg@40tude.net> Date: Wed, 28 Sep 2005 18:37:53 +0200 Message-ID: <1pcj2c5hcop7$.1m6hudk4iekuz$.dlg@40tude.net> NNTP-Posting-Date: 28 Sep 2005 18:37:45 MEST NNTP-Posting-Host: 8e5405fd.newsread2.arcor-online.net X-Trace: DXC=Y>Zi_VDfeXFndh];1CKbgLQ5U85hF6f;DjW\KbG]kaMHGSi?jHD8GO@f?_2kY88;IE[6LHn;2LCVNVVa[ZlQni_AY`g]EO;EC;L X-Complaints-To: abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:5243 Date: 2005-09-28T18:37:45+02:00 List-Id: On Wed, 28 Sep 2005 15:12:23 GMT, Tapio Kelloniemi wrote: > "Dmitry A. Kazakov" wrote: >>On 27 Sep 2005 20:00:56 -0400, Robert A Duff wrote: >> >>> Tapio Kelloniemi writes: >>> >>>> V1 : T := F; >>>> V2 : T renames F; >>>> >>>> I'd like to see an explanation of the effect of an object renaming >>>> declaration versus variable declaration and initialisation, when the object >>>> being renamed is a return value of a function. Many thanks for it. >>> >>> They are pretty similar. >> >>What about difference in respect to creation of new objects? > > Renaming creates a new object as does initialisation, No, it creates a view. > when not > returning by reference (return value is copied = new object is created). Try this with GNAT: with Ada.Finalization; package Foo is type T is new Ada.Finalization.Controlled with null record; procedure Finalize (X : in out T); function Create return T; end Foo; ------------------------------------ with Ada.Text_IO; package body Foo is procedure Finalize (X : in out T) is begin Ada.Text_IO.Put_Line ("Finalize"); end Finalize; function Create return T is begin return (Ada.Finalization.Controlled with null record); end Create; end Foo; --------------------------------- with Foo; use Foo; with Ada.Text_IO; procedure Baz is begin Ada.Text_IO.Put_Line ("Initialization:"); declare X : T := Create; begin null; end; Ada.Text_IO.Put_Line ("Renaming:"); declare X : T renames Create; begin null; end; end Baz; The output should look like: Initialization: Finalize -- X value created before ":=" Finalize -- A local object in Foo Finalize -- X value assigned by ":=" Renaming: Finalize -- A local object in Foo Finalize -- X value renamed A compiler has right to optimize out the local object. I believe, it also can optimize out initialization/finalization pair of the target in declaration with initialization, so in the end it could be same, though it isn't in GNAT. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de