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,f8a440310f7f2e02 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!novia!newsfeed.yul.equant.net!newspeer.monmouth.com!newsswitch.lcs.mit.edu!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Extended return question Date: Thu, 10 Jul 2008 10:37:55 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls6.std.com 1215700675 5139 192.74.137.71 (10 Jul 2008 14:37:55 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 10 Jul 2008 14:37:55 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:cmsuYwcUM7VrEqgKjgIyqP+EZuw= X-Original-Bytes: 3725 Xref: g2news1.google.com comp.lang.ada:1079 Date: 2008-07-10T10:37:55-04:00 List-Id: Dale Stanbrough writes: > The purpose of an extended return is to create the values in-situ > without copying. No, that's not correct. Limited function results are built in place, nonlimited ones are not. It's got nothing to do with which sort of return statement you use -- limited builds in place even when you say "return ;". The purpose of extended return is to give a name to the result, so you can poke at it, or assert things about it, etc. This capability is needed for limited types, but is also useful for nonlimited types. Your example below uses a nonlimited type, and it would be wrong for the compiler to use build-in-place in this case -- the left-hand side of an assignment must not be modified if the right-hand side raises an exception. An implementation can use build-in-place for nonlimited types, but only in cases where it can prove that there is no visible difference, such as when you are initializing a new object, or if there are no references to the object in any relevant exception handler. The example below is not such a case. I intend to make GNAT use build-in-place for nonlimited types in some cases someday, for efficiency. But it's low priority. - Bob > ------------------------------------------------- > with Ada.Text_IO; use Ada.Text_IO; > with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; > > procedure Returning_Large_Objects is > > type List is array (1..4) of Integer; > > > function Extended_Return_Error (Raise_Exception : Boolean) > return List > is > begin > return L : List do > L (1) := 1; L (2) := 2; > if Raise_Exception then raise Constraint_Error; end if; > L (3) := 3; L (4) := 4; > end return; > end; > > Destination : List; > > ------------------------------- > procedure Put (Item : List) is > begin > for i in Item'range loop > Put (Item (I)); > end loop; > New_Line; > end; > > begin > > -- run normally, no exception > > Destination := (others => 0); > > Put (Destination); > Destination := Extended_Return_Error (false); > Put (Destination); > > -- displays 1 2 3 4 as expected > > --------------------------------- > -- run with exception in middle > > Destination := (others => 0); > Put (Destination); > begin > Destination := Extended_Return_Error (true); > exception > when others => null; > end; > > Put (Destination); > -- displays 0 0 0 0, i expected 1 2 0 0 > > -- the exception seems to have affected statements > -- that precede it > > > end Returning_Large_Objects; > > -- > dstanbro@spam.o.matic.bigpond.net.au