comp.lang.ada
 help / color / mirror / Atom feed
* Equivalence between named access and anonymous access.
@ 2023-09-06 14:37 Blady
  2023-09-06 15:54 ` Dmitry A. Kazakov
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Blady @ 2023-09-06 14:37 UTC (permalink / raw)


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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Equivalence between named access and anonymous access.
  2023-09-06 14:37 Equivalence between named access and anonymous access Blady
@ 2023-09-06 15:54 ` Dmitry A. Kazakov
  2023-09-07 16:06   ` Blady
  2023-09-06 20:55 ` Gautier write-only address
  2023-09-07  0:20 ` Jeffrey R.Carter
  2 siblings, 1 reply; 8+ messages in thread
From: Dmitry A. Kazakov @ 2023-09-06 15:54 UTC (permalink / raw)


On 2023-09-06 16:37, Blady wrote:

> 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?

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Equivalence between named access and anonymous access.
  2023-09-06 14:37 Equivalence between named access and anonymous access Blady
  2023-09-06 15:54 ` Dmitry A. Kazakov
@ 2023-09-06 20:55 ` Gautier write-only address
  2023-09-07  0:20 ` Jeffrey R.Carter
  2 siblings, 0 replies; 8+ messages in thread
From: Gautier write-only address @ 2023-09-06 20:55 UTC (permalink / raw)


> 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;

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Equivalence between named access and anonymous access.
  2023-09-06 14:37 Equivalence between named access and anonymous access Blady
  2023-09-06 15:54 ` Dmitry A. Kazakov
  2023-09-06 20:55 ` Gautier write-only address
@ 2023-09-07  0:20 ` Jeffrey R.Carter
  2 siblings, 0 replies; 8+ messages in thread
From: Jeffrey R.Carter @ 2023-09-07  0:20 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Equivalence between named access and anonymous access.
  2023-09-06 15:54 ` Dmitry A. Kazakov
@ 2023-09-07 16:06   ` Blady
  2023-09-07 16:18     ` Jeffrey R.Carter
  2023-09-07 20:23     ` Dmitry A. Kazakov
  0 siblings, 2 replies; 8+ messages in thread
From: Blady @ 2023-09-07 16:06 UTC (permalink / raw)


Le 06/09/2023 à 17:54, Dmitry A. Kazakov a écrit :
> On 2023-09-06 16:37, Blady wrote:
> 
>> 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?
> 
> 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.
> 

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.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Equivalence between named access and anonymous access.
  2023-09-07 16:06   ` Blady
@ 2023-09-07 16:18     ` Jeffrey R.Carter
  2023-09-07 19:10       ` Blady
  2023-09-07 20:23     ` Dmitry A. Kazakov
  1 sibling, 1 reply; 8+ messages in thread
From: Jeffrey R.Carter @ 2023-09-07 16:18 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Equivalence between named access and anonymous access.
  2023-09-07 16:18     ` Jeffrey R.Carter
@ 2023-09-07 19:10       ` Blady
  0 siblings, 0 replies; 8+ messages in thread
From: Blady @ 2023-09-07 19:10 UTC (permalink / raw)


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.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Equivalence between named access and anonymous access.
  2023-09-07 16:06   ` Blady
  2023-09-07 16:18     ` Jeffrey R.Carter
@ 2023-09-07 20:23     ` Dmitry A. Kazakov
  1 sibling, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2023-09-07 20:23 UTC (permalink / raw)


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.

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-09-07 20:23 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-06 14:37 Equivalence between named access and anonymous access Blady
2023-09-06 15:54 ` Dmitry A. Kazakov
2023-09-07 16:06   ` Blady
2023-09-07 16:18     ` Jeffrey R.Carter
2023-09-07 19:10       ` Blady
2023-09-07 20:23     ` Dmitry A. Kazakov
2023-09-06 20:55 ` Gautier write-only address
2023-09-07  0:20 ` Jeffrey R.Carter

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