comp.lang.ada
 help / color / mirror / Atom feed
From: Jere <jhb.chat@gmail.com>
Subject: Return by reference
Date: Sun, 17 Dec 2017 06:38:13 -0800 (PST)
Date: 2017-12-17T06:38:13-08:00	[thread overview]
Message-ID: <a94cf715-a4a4-42bb-8800-9bb6a8baf9ac@googlegroups.com> (raw)

Implicit dereference has gone a long way in providing
a sort of return by reference capability in Ada.  It is
still fairly clunky to setup and use though and requires
a lot of definitions that aren't always easy to read
and discern their purpose without lots of extra comments.
Additionally, it still exposes access types in places where
a designer may not want to at all.  Yes, it does so more
safely than before, but I know at least for me, I like to
avoid exposing access types as much as possible.

I was wondering if Ada could take a step with it further
and provide a syntax for return by reference.  The building
blocks for implementation (Implicit_Dereference based
holders) are already in the language now.  All that I was
curious about was a way to remove the hard to read, clunky
setup and a way to remove direct access to the underlying
access type.

I don't know if I have any good ideas for the syntax honestly.
The one idea that I came up with probably has holes:

function Stuff(Object : Some_Aliased_Type) return aliased Component_Type;

Basically this would expand out to (in the compiler implementation):

type Component_Type_Holder(Raw : not null access Component_Type)
   is limited null record;

function Stuff(Object : Some_Aliased_Type) return Component_Type_Holder;

as for what could be returned:
1.  Any aliased or tagged package level object
2.  Any function parameter object (or it's components) provided
    that the parameter is either explicitly aliased or tagged

Local variables to the function would not be allowed as sources

so

function Stuff(Object : Some_Aliased_Type) return aliased Component_Type is
begin
   return Object.Component;
end Stuff;

would be ok but:

function Stuff(Object : Some_Aliased_Type) return aliased Component_Type is
   Temp : Some_Aliased_Type;
begin
   return Temp.Component;
end Stuff;

would not be ok. 

Calling a return by reference within a function that returns
by reference would not be allowed directly, but require an unchecked_access
so that they can easily be identified and provide the programmer with
opportunity to really consider the source of his/her return value.

Again, I'm not necessarily proposing this is the right syntax, I just
wanted something to get people thinking.  Any syntax/implementation that
can improve this functionality is welcome.  I'm more interested in
hiding the access discriminant and cutting back on all the overly clunky
(though currently necessary) declarations that reduce readability.

Other thoughts are completely welcome.

I know it isn't as easy as I laid out, but I fell like the current
implicit_dereference method now gives compiler writers at least
a starting tool to take it a step further and enhance readability
and safety (less access types exposed).

             reply	other threads:[~2017-12-17 14:38 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-17 14:38 Jere [this message]
2017-12-17 14:57 ` Return by reference Dmitry A. Kazakov
2017-12-18  0:51   ` Mehdi Saada
2017-12-18  8:18     ` Dmitry A. Kazakov
2017-12-18 10:47       ` Mehdi Saada
2017-12-18 12:16         ` Dmitry A. Kazakov
2017-12-18 22:41     ` Randy Brukardt
2017-12-18 22:33 ` Randy Brukardt
2017-12-19  1:29   ` Jere
  -- strict thread matches above, loose matches on Subject: below --
1999-11-01  0:00 Brian Rogoff
1999-11-02  0:00 ` Robert A Duff
1999-11-02  0:00   ` Brian Rogoff
1999-11-03  0:00     ` Matthew Heaney
1999-11-03  0:00       ` Matthew Heaney
1999-11-03  0:00       ` Brian Rogoff
1999-11-03  0:00     ` Lutz Donnerhacke
1999-11-03  0:00       ` Brian Rogoff
1999-11-04  0:00         ` Lutz Donnerhacke
1999-11-06  0:00           ` Brian Rogoff
1999-11-07  0:00             ` Lutz Donnerhacke
1999-11-04  0:00       ` Robert A Duff
1999-11-02  0:00 ` Lutz Donnerhacke
replies disabled

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