From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-3.2 required=3.0 tests=BAYES_00,NICE_REPLY_A, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Equivalence between named access and anonymous access. Date: Thu, 7 Sep 2023 22:23:37 +0200 Organization: A noiseless patient Spider Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 7 Sep 2023 20:23:35 -0000 (UTC) Injection-Info: dont-email.me; posting-host="3e4fde5204bf1fe9ab276fdba6340f70"; logging-data="3282190"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZLNPPgGHmTrP80Ga0M10aaPyU3EQlv28=" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Cancel-Lock: sha1:9nQOyz9FTIognXE0GCn9EqfKdrY= In-Reply-To: Content-Language: en-US Xref: news.eternal-september.org comp.lang.ada:65617 List-Id: 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