comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Extended return question
Date: Thu, 10 Jul 2008 10:37:55 -0400
Date: 2008-07-10T10:37:55-04:00	[thread overview]
Message-ID: <wccfxqh24ak.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: MrNoSpam-F95CC3.12114110072008@news-server.bigpond.net.au

Dale Stanbrough <MrNoSpam@bigpoop.net.au> 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 <expression>;".

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



  parent reply	other threads:[~2008-07-10 14:37 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-10  2:11 Extended return question Dale Stanbrough
2008-07-10  7:18 ` Georg Bauhaus
2008-07-10  7:32   ` Dale Stanbrough
2008-07-10 15:24     ` Adam Beneschan
2008-07-10 23:56       ` Dale Stanbrough
2008-07-10 23:20     ` Randy Brukardt
2008-07-11  0:03       ` Adam Beneschan
2008-07-11  0:50         ` Robert A Duff
2008-07-10 14:37 ` Robert A Duff [this message]
2008-07-10 15:19   ` Adam Beneschan
2008-07-10 18:36     ` Dmitry A. Kazakov
2008-07-11  0:43       ` Robert A Duff
2008-07-11  7:39         ` Dmitry A. Kazakov
2008-07-11  9:06           ` christoph.grein
2008-07-11 14:24             ` Dmitry A. Kazakov
2008-07-11  0:29     ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox