• Equivalence between named access and anonymous access.

    From Blady@21:1/5 to All on Wed Sep 6 16:37:08 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Blady on Wed Sep 6 17:54:42 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gautier write-only address@21:1/5 to All on Wed Sep 6 13:55:02 2023
    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;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeffrey R.Carter@21:1/5 to Blady on Thu Sep 7 02:20:02 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Blady@21:1/5 to All on Thu Sep 7 18:06:15 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeffrey R.Carter@21:1/5 to Blady on Thu Sep 7 18:18:11 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Blady@21:1/5 to All on Thu Sep 7 21:10:12 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Blady on Thu Sep 7 22:23:37 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)