comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Limited returns
Date: Mon, 23 Jun 2008 21:44:33 +0200
Date: 2008-06-23T21:44:33+02:00	[thread overview]
Message-ID: <18qs48vm598lw$.1xvopewoc5l47$.dlg@40tude.net> (raw)
In-Reply-To: 0abe896d-2f42-43e6-8f8f-842dd5235950@f24g2000prh.googlegroups.com

On Mon, 23 Jun 2008 11:15:23 -0700 (PDT), Adam Beneschan wrote:

> On Jun 23, 10:03 am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
> wrote:
>>
>> Is there an AI to finally fix a similar problem with pool-specific access
>> types? I mean this:
>>
>> procedure Test_Access is
>>    type T is tagged null record;
>>    type S is new T with null record;
>>
>>    type T_Ptr is access T'Class;
>>    type S_Ptr is access S'Class;
>>
>>    function Factory return T_Ptr is
>>       X : S_Ptr := new S;
>>    begin
>>       .. -- Do something with X
>>       return T_Ptr (X); -- Illegal!???
>>    end Factory;
>> begin
>>    null;
>> end Test_Access;
>>
>> The only workaround is to cast:
>>
>>    function Factory return T_Ptr is
>>       Result : P_Ptr := new S;
>>       X : S'Class renames S'Class (Result.all);
>>    begin
>>       .. -- Do something with X
>>       return Result;
>>    end Factory;
> 
> I don't see an AI for this.  I'm guessing that the problem would be
> that you could specify different pools for the two different access
> types, which would make the type conversion problematic (you'd have an
> access type object that points into the wrong pool, which could then
> cause havoc if you do an Unchecked_Deallocation).

Clearly the conversion should simply raise Constraint_Error when pools
aren't same.

> If the language
> rules said that a type conversion like this were allowed only if the
> storage pools were the same, the problem would then be that, since a
> storage pool rep clause could appear in the private part of a package,
> you could have a situation where you could not tell whether a type
> conversion was legal without peeking into the private part of a
> package you don't have visibility to.  And that's something the
> language designers try very hard to avoid.

It is just like with tags, they too cannot be always statically checked.

> On the other hand, if you're not going to specify a Storage_Pool for
> the types, is there a reason to make them pool-specific?  I'm not sure
> what's gained by doing so.

1. Safety. It guarantees no objects on the stack.

2. If an access type is a generic parameter, then to have it general access
is a stronger precondition than necessary. It would be a bad design.

3. There are important cases when pools are needed. For example, to have a
container of indefinite by-value objects. If you don't want the overhead of
lists of descriptors, a custom pool is the just the way. (In some cases
streams would also work, but pools are more universal and efficient)

4. Pool-specific access could be more efficient.

------------
There is a related problem, that when a pool-specific object is initialized
or finalized, there is no way to get a pool-specific access to it. One have
to use Unchecked_Conversion of a general access type, which is not even
guaranteed to work.

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



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

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-23 14:21 Limited returns Dmitry A. Kazakov
2008-06-23 15:04 ` fedya_fedyakoff
2008-06-23 15:20   ` fedya_fedyakoff
2008-06-23 16:53     ` Dmitry A. Kazakov
2008-06-24 10:56       ` fedya_fedyakoff
2008-06-24 13:51         ` Dmitry A. Kazakov
2008-06-24 15:01           ` fedya_fedyakoff
2008-06-24 16:31             ` Dmitry A. Kazakov
2008-06-24 16:42               ` Dmitry A. Kazakov
2008-06-23 15:49   ` Adam Beneschan
2008-06-24 10:13     ` fedya_fedyakoff
2008-06-23 15:15 ` Adam Beneschan
2008-06-23 17:03   ` Dmitry A. Kazakov
2008-06-23 18:15     ` Adam Beneschan
2008-06-23 19:44       ` Dmitry A. Kazakov [this message]
2008-06-26 12:35 ` Egil Høvik
2008-06-26 14:12   ` Dmitry A. Kazakov
replies disabled

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