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!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!gandalf.srv.welterde.de!news.jacob-sparre.dk!loke.jacob-sparre.dk!pnx.dk!.POSTED!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: Should weak counted reference be able to be strengthened? Date: Fri, 21 Nov 2014 16:39:25 -0600 Organization: Jacob Sparre Andersen Research & Innovation Message-ID: References: <7d9r3whe43z8$.1f2l3n5cz0zh0$.dlg@40tude.net> NNTP-Posting-Host: rrsoftware.com X-Trace: loke.gir.dk 1416609567 10530 24.196.82.226 (21 Nov 2014 22:39:27 GMT) X-Complaints-To: news@jacob-sparre.dk NNTP-Posting-Date: Fri, 21 Nov 2014 22:39:27 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 Xref: news.eternal-september.org comp.lang.ada:23621 Date: 2014-11-21T16:39:25-06:00 List-Id: "Dmitry A. Kazakov" 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.