comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: GNAT - return by anonymous access
Date: Wed, 21 Dec 2011 23:11:31 -0800 (PST)
Date: 2011-12-21T23:11:31-08:00	[thread overview]
Message-ID: <6e5ee357-7409-4974-917c-d7236c782d31@h11g2000yqd.googlegroups.com> (raw)
In-Reply-To: 4f78ef9f-ca8d-43ef-ab71-0f775fbeebd9@z19g2000vbe.googlegroups.com

On Dec 21, 4:33 pm, Simon Belmont <sbelmont...@gmail.com> wrote:
> On Dec 21, 2:29 am, AdaMagica <christ-usch.gr...@t-online.de> wrote:
>
> > Hm, no, return-by-reference has been replaced by build-in-place.
>
> Building in place creates another object, does it not?  Changes to a
> returned built in place type would not affect the original object.
> Suppose I have a package with some internal object of a limited, not
> necessarily allocated, but that has to get exposed to other units:
>
> package body Sample is
>
>   foo : Lim_Type_Ptr := -- ...stuff
>
>   function Get return Lim_Type is
>   begin
>     return foo.all;
>   end Sample;
>
> The above code, which as I understand it would be legal Ada95 code,
> doesn't return the actual object, but returns a pointer that the
> compiler automatically dereferences behind the scenes.  The client
> can't change, copy, or delete the object or the access value because
> it cannot reference it.
>
> In the Ada05 version, it would have to return a Lim_Type_Ptr (or,
> equivilently, an access Lim_Type), and the client would have to
> dereference it.  Now the client has free means to change, copy, and
> deallocate the access value.  At best it works the same, and at worst
> an errant programmer can wreak havoc.

I think this could be solved by declaring a private type (limited
private, if you prefer) to represent an "access" or "handle" or
"reference" to test_type.  It would be implemented as an access type,
basically, but the client wouldn't see it as an access type, so it
couldn't deallocate it, and if you make it limited private it couldn't
copy it either.  The only thing you lose is that since it's private,
you couldn't directly use it to access components of the accessed type
(test_type).  But in your example, test_type is private anyway, so
this isn't much of a loss, except for the ability to access the
discriminant p_obj, and you could provide a function to do that.


> I suppose my question is why?  No one seems to have a valid technical
> reason for this change, other than returning-by-reference is
> 'unusual', which doesn't seem like nearly enough of a good reason to
> take something that was working fine and make it more dangerous.

You might want to check out

http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ais/ai-10318.txt?rev=1.16

just to convince yourself that, yes, they *did* have plenty of valid
technical reasons.  Even if this is hard to follow, at least the fact
that they had as much discussion as they did should make it clear that
this wasn't something the ARG did just because they got drunk and felt
like changing something.

                             -- Adam



  reply	other threads:[~2011-12-22  7:11 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-17 19:17 GNAT - return by anonymous access Simon Belmont
2011-12-17 19:51 ` Simon Wright
2011-12-18  9:08 ` Georg Bauhaus
2011-12-19 23:46 ` Randy Brukardt
2011-12-20  0:14   ` Shark8
2011-12-20 23:18     ` Randy Brukardt
2011-12-21  0:44       ` Simon Belmont
2011-12-21  7:29         ` AdaMagica
2011-12-21 11:05           ` Georg Bauhaus
2011-12-21 11:43             ` AdaMagica
2011-12-21 12:05               ` Georg Bauhaus
2011-12-22  0:33                 ` Randy Brukardt
2011-12-22  0:33           ` Simon Belmont
2011-12-22  7:11             ` Adam Beneschan [this message]
2011-12-22 22:11               ` Simon Belmont
2011-12-23  0:56                 ` Randy Brukardt
2011-12-23 13:20                   ` Simon Belmont
2011-12-27 23:51                     ` Randy Brukardt
2011-12-20 23:53 ` anon
replies disabled

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