From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Overloading operator "=" for anonymous access types?
Date: Tue, 15 Jan 2019 09:38:11 +0100
Date: 2019-01-15T09:38:11+01:00 [thread overview]
Message-ID: <q1k65j$1qqm$1@gioia.aioe.org> (raw)
In-Reply-To: q1j4pi$2h0$1@franka.jacob-sparre.dk
On 2019-01-15 00:08, Randy Brukardt wrote:
> <daicrkk@googlemail.com> 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
next prev parent reply other threads:[~2019-01-15 8:38 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-11 21:46 Overloading operator “=” for anonymous access types? daicrkk
2019-01-12 9:50 ` Simon Wright
2019-01-12 14:01 ` Simon Wright
2019-01-12 15:15 ` daicrkk
2019-01-14 23:08 ` Overloading operator "=" " Randy Brukardt
2019-01-15 0:34 ` Shark8
2019-01-15 8:38 ` Dmitry A. Kazakov [this message]
2019-01-15 21:00 ` Randy Brukardt
2019-01-16 15:42 ` Dmitry A. Kazakov
2019-01-15 8:51 ` daicrkk
2019-01-15 11:15 ` Simon Wright
2019-01-17 3:20 ` Jere
2019-01-17 8:23 ` Dmitry A. Kazakov
2019-01-17 22:22 ` Randy Brukardt
2019-01-18 10:17 ` Dmitry A. Kazakov
2019-01-18 13:27 ` Jere
2019-01-18 13:42 ` Dmitry A. Kazakov
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox