Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Equivalence between named access and anonymous access.

57 views
Skip to first unread message

Blady

unread,
Sep 6, 2023, 10:37:13 AM9/6/23
to
Hello,

I'm wondering about named access and anonymous access.
In the following Ada code, are the writing of parameter P1 type of
procedures PA and PB equivalent ?

package C1 is
type Inst is tagged null record;
type Class is access all Inst'Class;
end C1;

with C1;
package C2 is
type Inst is tagged null record;
type Class is access all Inst'Class;

procedure PA (Self : Inst; P1 : C1.Class); -- named access
procedure PB (Self : Inst; P1 : access C1.Inst'Class); -- anonymous
access
end C2;

Same with:
function FA (Self : Inst) return C1.Class; -- named access
function FB (Self : Inst) return access C1.Inst'Class; -- anonymous
access

Are FA and FB writing equivalent?
If not why?

Thanks, Pascal.

Dmitry A. Kazakov

unread,
Sep 6, 2023, 11:54:45 AM9/6/23
to
They are not equivalent from the access checks point of view:

declare
Y : C2.Inst;
X : aliased C1.Inst;
begin
C2.PA (Y, X'Access); -- Non-local pointer error
C2.PB (Y, X'Access); -- Fine
end;

Furthermore, tagged anonymous access is controlling (dispatches) when
not class-wide.

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

Gautier write-only address

unread,
Sep 6, 2023, 4:55:04 PM9/6/23
to
> In the following Ada code, are the writing of parameter P1 type of
> procedures PA and PB equivalent ?

They are not equivalent because the anonymous access opens more possibilities (example below), but you are certainly aware of that.
So I guess you have another question in mind...

with C1, C2;

procedure test is
x2 : C2.Inst;
type My_Reference_1 is access all C1.Inst'Class;
r1 : My_Reference_1;
begin
x2.PB (r1);
x2.PA (r1);
-- ^ expected type "Class" defined at c1.ads:3
-- found type "My_Reference_1" defined at line 6
end;

Jeffrey R.Carter

unread,
Sep 6, 2023, 8:20:05 PM9/6/23
to
On 2023-09-06 16:37, Blady wrote:
>
> I'm wondering about named access and anonymous access.

The rules for using access-to-object types are

1. Don't use access types
2. If you think you should use access types, see rule 1.
3. If you still think you should use access types, don't use anonymous access types
4. If you still think you should use anonymous access types, don't develop software

The semantics of named access types are well defined and easily understood. The
semantics of anonymous access types are defined in ARM 3.10.2, of which the AARM
says

"Subclause 3.10.2, home of the accessibility rules, is informally known as the
'Heart of Darkness' amongst the maintainers of Ada. Woe unto all who enter here
(well, at least unto anyone that needs to understand any of these rules)."

The ARG freely admits that no one understands 3.10.2, which means that what you
get when you use anonymous access types is whatever the compiler writer thinks
it says. This may differ between compilers and between different versions of the
same compiler, and from what you think it says.

So no sane person uses them.

--
Jeff Carter
“Companies who create critical applications—those
with a low tolerance for risk—would do well to use
Ada for those applications, even if they're more
familiar with other languages like C and C++.”
Mike Jelks
207

Blady

unread,
Sep 7, 2023, 12:06:20 PM9/7/23
to
Thanks Dmitry, also Gautier and Jeff for your previous answers,

Well, I was questioning myself about the choice between named access and
anonymous access in the old Ada port of Java library, for instance:

type Typ;
type Ref is access all Typ'Class;
type Typ(LayoutManager2_I : Java.Awt.LayoutManager2.Ref;
Serializable_I : Java.Io.Serializable.Ref)
is new Java.Lang.Object.Typ
with null record;
------------------------------
-- Constructor Declarations --
------------------------------
function New_BorderLayout (This : Ref := null)
return Ref;

function New_BorderLayout (P1_Int : Java.Int;
P2_Int : Java.Int;
This : Ref := null)
return Ref;
-------------------------
-- Method Declarations --
-------------------------
procedure AddLayoutComponent (This : access Typ;
P1_Component : access
Standard.Java.Awt.Component.Typ'Class;
P2_Object : access
Standard.Java.Lang.Object.Typ'Class);
function GetLayoutComponent (This : access Typ;
P1_Object : access
Standard.Java.Lang.Object.Typ'Class)
return access Java.Awt.Component.Typ'Class;


Why choosing named access for New_BorderLayout and anonymous access for
AddLayoutComponent or GetLayoutComponent for the type of parameters
P1_xxx and the return type?
Why not all named or all anonymous ?

Thanks, Pascal.



Jeffrey R.Carter

unread,
Sep 7, 2023, 12:18:14 PM9/7/23
to
On 2023-09-07 18:06, Blady wrote:
>
> Why choosing named access for New_BorderLayout and anonymous access for
> AddLayoutComponent or GetLayoutComponent for the type of parameters P1_xxx and
> the return type?

It's very poor design to have access types in the visible part of a non-private
pkg spec.

--
Jeff Carter
"This language [Ada] has a remarkable power of expressiveness,
something vital to the rapid development of complicated software,
and its 'strong typing' makes it easy to debug and modify."
Scott and Bagheri
160

Blady

unread,
Sep 7, 2023, 3:10:16 PM9/7/23
to
Le 07/09/2023 à 18:18, Jeffrey R.Carter a écrit :
> On 2023-09-07 18:06, Blady wrote:
>>
>> Why choosing named access for New_BorderLayout and anonymous access
>> for AddLayoutComponent or GetLayoutComponent for the type of
>> parameters P1_xxx and the return type?
>
> It's very poor design to have access types in the visible part of a
> non-private pkg spec.
>

Hello Jeff,

I got you point :-)

But, in this specific case, I was wondering why not writing all with
named access or all with anonymous access?

Pascal.


Dmitry A. Kazakov

unread,
Sep 7, 2023, 4:23:38 PM9/7/23
to
On 2023-09-07 18:06, Blady wrote:

> Well, I was questioning myself about the choice between named access and
> anonymous access in the old Ada port of Java library, for instance:
>
>    type Typ;
>    type Ref is access all Typ'Class;
>    type Typ(LayoutManager2_I : Java.Awt.LayoutManager2.Ref;
>             Serializable_I : Java.Io.Serializable.Ref)
>     is new Java.Lang.Object.Typ
>       with null record;
>    ------------------------------
>    -- Constructor Declarations --
>    ------------------------------
>    function New_BorderLayout (This : Ref := null)
>                               return Ref;

Contravariance is unsafe. I gather that Typ is tagged. If you ever
derive from it, it will "inherit" the broken construction function,
because the function is class-wide. The safe choice here is anonymous
access. The compiler will require to override the construction function.
That is for the return value. The case for the argument depends. Again
anonymous access type is more handy but if you going to copy/store
references, then named types are better.

> Why not all named or all anonymous ?

My rough rule is like this:

Do not expose access types if you can.

If you successfully hidden them either completely or by declaring them
private, then named they go.

If you exposed access types, then anonymous access is usually a better
choice because it is easier to use, especially when access is merely to
work around language limitations on argument/result passing
(unconstrained object, access rules nightmare) AKA closures. Then it
much is safer in a hierarchy of types and it is more use-clause friendly.
0 new messages