From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Should weak counted reference be able to be strengthened?
Date: Fri, 21 Nov 2014 16:39:25 -0600
Date: 2014-11-21T16:39:25-06:00 [thread overview]
Message-ID: <m4oeuv$a92$1@loke.gir.dk> (raw)
In-Reply-To: kywrzxvibzmw$.1h82z2coiciqh$.dlg@40tude.net
"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message
news:kywrzxvibzmw$.1h82z2coiciqh$.dlg@40tude.net...
> On Fri, 21 Nov 2014 15:00:12 +0000 (UTC), Natasha Kerensikova wrote:
>
>> The strong reference is hidden in the accessor object. Consider for
>> example the following specification fragments, where T is the formal
>> type of the referred objects:
>>
>> type Accessor (Data : not null access constant T) is limited private;
>>
>> type Strong_Reference is tagged private;
>>
>> function Query (Ref : Strong_Reference) return Accessor;
>>
>> type Weak_Reference is tagged private;
>>
>> function Weaken (Parent : Strong_Reference'Class) return
>> Weak_Reference;
>> -- 'Class only because we are still in the same package
>>
>> function Query (Ref : Weak_Reference) return Accessor;
>>
>> private
>>
>> type Accessor (Data : not null access constant T) is limited record
>> Parent : Strong_Reference;
>> end record;
>
> Why do you need accessors? For Ada's crazy implicit dereference?
It's not crazy, it's brilliant. ;-) There's really no other way to make
things both easy-to-use and safe.
Anyway, I think Natasha was right initially. The only strong references that
ought to exist are those associated with the accessor type, and those
clearly will be short-lived and not going to end up circular. That's
essentially the model used the the Ada.Containers (where a Cursor is a weak
reference, and the tampering check is used to ensure it is a strong
reference while an accessor or iterator exists).
Indeed, the easiest thing to do is to rename Accessor to Strong_Reference,
and all is solved. :-)
The real question is whether there is a real need to have a long-lived
Strong_Reference. I would argue against such a thing, but I don't think it
is clearly an open-and-shut case.
>> The only way to actually reach a referred object is by dereferencing the
>> access discriminant of an Accessor value. The strong reference inside
>> Accessor objects ensure that the referred object will outlive the
>> Accessor object.
>
> It is much simpler to return a plain access type:
>
> function Ptr (Ref : Strong_Reference) return not null access T['Class];
Sure, but now you have an extra function with no real purpose. If you use
the accessor form, you don't need any way at all to get the pointer, you
always have access to it. As I said, the most sensible thing is to rename
type Accessor to Strong_Reference and all is well (especially as you usually
won't explicitly create one of these, so the name is irrelevant).
And anytime you have a bare pointer, you have the possibility of it becoming
dangling because they're easy to copy. My preference would be to never
expose it at all, but of course that causes problems for ease-of-use.
Randy.
next prev parent reply other threads:[~2014-11-21 22:39 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-21 11:07 Should weak counted reference be able to be strengthened? Natasha Kerensikova
2014-11-21 13:16 ` Dmitry A. Kazakov
2014-11-21 15:00 ` Natasha Kerensikova
2014-11-21 16:50 ` Dmitry A. Kazakov
2014-11-21 18:24 ` Natasha Kerensikova
2014-11-21 19:36 ` Dmitry A. Kazakov
2014-11-21 22:28 ` Randy Brukardt
2014-11-22 9:57 ` Natasha Kerensikova
2014-11-22 10:19 ` Dmitry A. Kazakov
2014-11-22 10:36 ` Natasha Kerensikova
2014-11-22 11:15 ` Dmitry A. Kazakov
2014-11-21 22:39 ` Randy Brukardt [this message]
2014-11-21 22:49 ` Shark8
2014-11-22 8:42 ` Dmitry A. Kazakov
2014-11-22 8:42 ` Dmitry A. Kazakov
2014-11-24 22:15 ` Randy Brukardt
2014-11-25 8:36 ` Dmitry A. Kazakov
2014-11-25 21:49 ` Randy Brukardt
2014-11-26 9:28 ` Dmitry A. Kazakov
2014-11-26 17:51 ` brbarkstrom
2014-11-26 21:09 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox