comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: another way to shoot yourself in the foot?
Date: Tue, 24 Jun 2008 19:24:22 -0400
Date: 2008-06-24T19:24:22-04:00	[thread overview]
Message-ID: <wccmylav2l5.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: CCd8k.224226$yE1.183360@attbi_s21

"Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> writes:

> Of course you're passing a reference, since limited types are always
> passed by reference (since Ada 95). But I don't think that the object
> had to exist before the call to F:

Yes, you're right.  I was wrong to say it had to exist before.
To give the precise answer, I'd have to talk about "accessibility
levels".  Basically, in Ada 95, F returns an implicit reference
to something "outside of" F -- outside in the sense of having
a longer potential lifetime.

In Ada 2005, it's just the opposite -- F returns something created by F.

> procedure Task_Return_Test is
>    package Wrap is
>       task type T;
>
>       function F return T;
>    end Wrap;
>
>    package body Wrap is
>       type T_Ptr is access T;
>
>       function F return T is
>          R : T_Ptr := new T;
>       begin -- F
>          return R.all;
>       end F;
>
>       task body T is
>          -- null;
>       begin -- T
>          null;
>       end T;
>    end Wrap;
>
>    procedure P (V : in Wrap.T) is
>       -- null;
>    begin -- P
>       null;
>    end P;
> begin -- Task_Return_Test
>    P (V => Wrap.F);
> end Task_Return_Test;
>
> This is valid Ada 95, according to a compiler.

It is also valid Ada 95, according to the Ada 95 Reference Manual.  ;-)
It is illegal in Ada 2005.

I claim that it would be clearer (in Ada 95) for F to return an
access-to-T, since that's what is really going on.  And in Ada 2005,
you have to do it that way.

>> Well, there are only two ways a function call can be used: to initialize
>> a new object, or to update an existing object (i.e. on the right-hand
>> side of an assignment statement).  But don't forget there are a dozen or
>> so different ways of creating new objects (components, parameters,
>> parent parts of aggregates, etc).  All of these are allowed for
>> build-in-place function calls.  The only thing that's not allowed is the
>> assignment statement.
>> You can still say:
>>     P(F(...));
>> where F returns a limited type.  Yes, there's a new object being
>> created
>> (the formal parameter of P), and F is being used to initialize that
>> object (in place!).  In Ada 2005, functions always return newly-created
>> objects, which I think is appropriate.
>
> I learned that functions return objects, and in this case the object is
> the actual parameter to P, not the formal parameter. The object is
> anonymous.

No, that's not the right way to think about it (for limited types!).
The whole point of build-in-place is that the object created inside the
function at the "return", and the function result, and the actual
parameter of P, and the formal parameter of P -- are all one and the
same object!

If F said "return G(...);" and G said "return H(...);" we've got a whole
bunch of objects that are one and the same object, in the end.

As somebody mentioned, there's an AI that tries to clarify the wording
on this point.

>...Maybe that rule's changed for limited parameters in current
> Ada. But I take your point that F can be regarded as always building in
> place, wherever that place may be.

OK.

>>     if F(...).Flag then ...
>> The last one is pretty silly -- it creates a new limited object,
>> grabs a boolean flag out of it, and then throws the whole thing
>> away.
>
> It's also an example in which the object that F builds in is clearly
> anonymous, unlike the parameter of P above.

Right, at the call site, it's anonymous.  It might have a name inside F
(if F said "return Result : ...") or be anonymous inside F (if F said
"return <expression>"), but either way, it's one object.

- Bob



  reply	other threads:[~2008-06-24 23:24 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-20  9:03 another way to shoot yourself in the foot? fedya_fedyakoff
2008-06-20  9:34 ` Dmitry A. Kazakov
2008-06-20  9:48   ` fedya_fedyakoff
2008-06-20 10:01     ` Ludovic Brenta
2008-06-20 10:05 ` christoph.grein
2008-06-20 10:26   ` Dmitry A. Kazakov
2008-06-20 16:12     ` Adam Beneschan
2008-06-20 15:48   ` Adam Beneschan
2008-06-20 19:27   ` Robert A Duff
2008-06-20 23:37     ` Jeffrey R. Carter
2008-06-21  8:56       ` Dmitry A. Kazakov
2008-06-22 20:44         ` Robert A Duff
2008-06-23  7:49           ` Dmitry A. Kazakov
2008-06-24  4:02             ` george.priv
2008-06-24  7:30               ` Dmitry A. Kazakov
2008-06-24 17:16                 ` Robert A Duff
2008-06-24 19:15                   ` Jeffrey R. Carter
2008-06-24 20:31                     ` Robert A Duff
2008-06-24 20:50                       ` Ludovic Brenta
2008-06-24 23:02                         ` Robert A Duff
2008-06-24 23:42                         ` Georg Bauhaus
2008-06-24 21:24                       ` Jeffrey R. Carter
2008-06-24 23:24                         ` Robert A Duff [this message]
2008-06-25 15:07                       ` Adam Beneschan
2008-06-24 14:59             ` Adam Beneschan
2008-06-24 16:41               ` Dmitry A. Kazakov
2008-06-24 17:20                 ` Robert A Duff
2008-06-24 17:52                   ` Dmitry A. Kazakov
2008-06-24 23:35                     ` Georg Bauhaus
2008-06-25  8:09                       ` Dmitry A. Kazakov
2008-06-25 10:32                         ` Georg Bauhaus
2008-06-25 12:06                           ` Dmitry A. Kazakov
2008-06-22 20:37       ` Robert A Duff
2008-06-22 21:25         ` Jeffrey R. Carter
2008-07-04 20:52           ` Colin Paul Gloster
2008-07-04 22:15             ` (see below)
2008-07-05 16:06               ` Colin Paul Gloster
2008-07-05 13:38             ` Gary Scott
2008-07-05 16:42               ` Colin Paul Gloster
2008-07-05 19:00                 ` Gary Scott
2008-07-09 19:39                   ` Colin Paul Gloster
2008-07-09 20:35                     ` Richard Maine
2008-07-09 22:49                       ` Terence
2008-07-10  1:07                         ` Gary Scott
2008-07-10 14:10                       ` Colin Paul Gloster
2008-07-10 14:57                         ` fj
2008-07-10 16:47                           ` Richard Maine
2008-07-10 17:03                         ` Dick Hendrickson
2008-07-10 17:26                           ` Craig Powers
2008-07-10 19:55                             ` James Giles
2008-07-10 20:45                               ` Dick Hendrickson
2008-07-10 21:22                                 ` Richard Maine
2008-07-10 21:29                                   ` Craig Powers
2008-07-10 20:45                               ` Craig Powers
2008-07-10 19:51                           ` James Giles
2008-07-11 15:02                             ` Colin Paul Gloster
replies disabled

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