From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Overloading operator "=" for anonymous access types? Date: Tue, 15 Jan 2019 09:38:11 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <167dc83d-daac-49eb-ba79-48866ccde39d@googlegroups.com> <0c56d9f4-8861-4c74-b170-a973e3789b08@googlegroups.com> NNTP-Posting-Host: p9a8jKAGz0rpkSSbWxF1gQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.8.3 Xref: reader01.eternal-september.org comp.lang.ada:55279 Date: 2019-01-15T09:38:11+01:00 List-Id: On 2019-01-15 00:08, Randy Brukardt wrote: > wrote in message > news:0c56d9f4-8861-4c74-b170-a973e3789b08@googlegroups.com... > >> I second that. Access Cell is an access-to-object type whose designated >> type is Cell (check), Cell has a user-defined primitive equality operator >> (check) such that its result type is Boolean (check), it is declared >> immediately within the same declaration list as Cell (check), at least one >> of its operands is an access parameter with designated type Cell (both >> operands are, check). >> According to 4.5.2, universal_access "=" should never be allowed to kick in >> at all here, not even with "L = null". Or am I missing something? > > Yup, I agree with this. My first thought when reading that example is that > it is wrong, because I don't remember anywhere in Ada where the same > operator with arguments of the same type means different things. I don't > think the use of "null" could change that. But the types are not same. It is universal_access vs. access. > Dunno if John wrote that for a different version of Ada, or he was just > confused by a rule that barely makes sense anyway. > > As always, best avoid anonymous access types unless you have to use one of > their special features (dynamic accessibility, dispatching, special > discriminant accessibility, or closures [for access-to-subprograms]). And > better still, lets lobby to get those special features optionally available > for named access types so no one every has to use an anonymous anything. :-) Named or anonymous it makes little difference, IMO. Here is a classic multi-method case. "=" is such an operation. null is universal_access (4.2). For any access type P there are 3 equality operations "=": function "=" (Left, Right : universal_access) return Boolean; type P is access T; function "=" (Left : P; Right : universal_access) return Boolean; function "=" (Left : universal_access; Right : P) return Boolean; function "=" (Left, Right : P) return Boolean; When the last one is overridden, what happens with the second and the third? One of three possibilities: 1. It inherits the base operation: function "=" (Left : P; Right : universal_access) return Boolean is begin return universal_access (Left) = Right; end "="; 2. It silently overrides: function "=" (Left : P; Right : universal_access) return Boolean is begin return Left = P (Right); end "="; 3. It gets overridden abstract and comparison to null becomes illegal because the operation is not defined. [ The reference manual is shy to say anything about it. It claims that universal_access is kind of class-wide, which would mean, if taken seriously, that "=" overloads and must clash with the original "=". Since it does not, universal_access is more like a parent type than class-wide.] It seems that in the OP's case as in the case with named access types #2 is in effect, which is illogical, inconsistent, unsafe, but would be expected by most people. Barnes' code presumes rather #1, which is logical, but confusing and error-prone. #3 would be consistent and safe: if Ptr_Value = Ptr_Type (null) then -- Type conversion required But it would not work with anonymous access types. So, if #3 were adopted, then overriding for anonymous types must be banished. All this is fine and good, except that overriding function "=" (Left, Right : access T) return Boolean; is also a primitive of T! You cannot banish it. P.S. And, wouldn't it be better to fix the type system, no? -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de