comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Equivalence between named access and anonymous access.
Date: Thu, 7 Sep 2023 22:23:37 +0200	[thread overview]
Message-ID: <uddbg7$3458e$1@dont-email.me> (raw)
In-Reply-To: <udcsdo$31uib$1@dont-email.me>

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

  parent reply	other threads:[~2023-09-07 20:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2023-09-06 20:55 ` Gautier write-only address
2023-09-07  0:20 ` Jeffrey R.Carter
replies disabled

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