comp.lang.ada
 help / color / mirror / Atom feed
* Renaming versus initialisation
@ 2005-09-27 21:10 Tapio Kelloniemi
  2005-09-28  0:00 ` Robert A Duff
  0 siblings, 1 reply; 6+ messages in thread
From: Tapio Kelloniemi @ 2005-09-27 21:10 UTC (permalink / raw)


Hi

Assuming the following declarations:

type T is ...;

function F return T is ...;

I'm a bit uncertain about the difference of the following declarations:

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.

-- 
Tapio



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Renaming versus initialisation
  2005-09-27 21:10 Renaming versus initialisation Tapio Kelloniemi
@ 2005-09-28  0:00 ` Robert A Duff
  2005-09-28  8:02   ` Dmitry A. Kazakov
  0 siblings, 1 reply; 6+ messages in thread
From: Robert A Duff @ 2005-09-28  0:00 UTC (permalink / raw)


Tapio Kelloniemi <invalid@localhost.localdomain.com> 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.  Differences: V2 is a constant, but V1 is a
variable.  In Ada 95, V2 works for limited types, but V1 does not.
The rules about limited types are relaxed in Ada 2005.

- Bob



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Renaming versus initialisation
  2005-09-28  0:00 ` Robert A Duff
@ 2005-09-28  8:02   ` Dmitry A. Kazakov
  2005-09-28 15:12     ` Tapio Kelloniemi
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2005-09-28  8:02 UTC (permalink / raw)


On 27 Sep 2005 20:00:56 -0400, Robert A Duff wrote:

> Tapio Kelloniemi <invalid@localhost.localdomain.com> 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?

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Renaming versus initialisation
  2005-09-28  8:02   ` Dmitry A. Kazakov
@ 2005-09-28 15:12     ` Tapio Kelloniemi
  2005-09-28 16:37       ` Dmitry A. Kazakov
  0 siblings, 1 reply; 6+ messages in thread
From: Tapio Kelloniemi @ 2005-09-28 15:12 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote:
>On 27 Sep 2005 20:00:56 -0400, Robert A Duff wrote:
>
>> Tapio Kelloniemi <invalid@localhost.localdomain.com> 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, when not
returning by reference (return value is copied = new object is created).

-- 
Tapio



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Renaming versus initialisation
  2005-09-28 15:12     ` Tapio Kelloniemi
@ 2005-09-28 16:37       ` Dmitry A. Kazakov
  2005-09-28 18:53         ` Jeffrey R. Carter
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2005-09-28 16:37 UTC (permalink / raw)


On Wed, 28 Sep 2005 15:12:23 GMT, Tapio Kelloniemi wrote:

> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote:
>>On 27 Sep 2005 20:00:56 -0400, Robert A Duff wrote:
>>
>>> Tapio Kelloniemi <invalid@localhost.localdomain.com> 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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Renaming versus initialisation
  2005-09-28 16:37       ` Dmitry A. Kazakov
@ 2005-09-28 18:53         ` Jeffrey R. Carter
  0 siblings, 0 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2005-09-28 18:53 UTC (permalink / raw)


Dmitry A. Kazakov wrote:

> 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.

I think this analysis is correct. This is compiler dependent. The only 
language-required difference is that renames creates a constant and 
initialization can create a variable.

-- 
Jeff Carter
"You tiny-brained wipers of other people's bottoms!"
Monty Python & the Holy Grail
18



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-09-28 18:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-27 21:10 Renaming versus initialisation Tapio Kelloniemi
2005-09-28  0:00 ` Robert A Duff
2005-09-28  8:02   ` Dmitry A. Kazakov
2005-09-28 15:12     ` Tapio Kelloniemi
2005-09-28 16:37       ` Dmitry A. Kazakov
2005-09-28 18:53         ` Jeffrey R. Carter

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