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!feeder.eternal-september.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!newsfeed.xs3.de!io.xs3.de!news.jacob-sparre.dk!franka.jacob-sparre.dk!pnx.dk!.POSTED.rrsoftware.com!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: Ada Alternatives to Unrestricted_Access Date: Mon, 12 Mar 2018 18:30:26 -0500 Organization: JSA Research & Innovation Message-ID: References: <4ab69a18-5766-446c-85c2-14e094199c95@googlegroups.com> <6792fcd7-a25a-417c-b45a-1a17b0168234@googlegroups.com> Injection-Date: Mon, 12 Mar 2018 23:30:26 -0000 (UTC) Injection-Info: franka.jacob-sparre.dk; posting-host="rrsoftware.com:24.196.82.226"; logging-data="29764"; mail-complaints-to="news@jacob-sparre.dk" 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.7246 Xref: reader02.eternal-september.org comp.lang.ada:50946 Date: 2018-03-12T18:30:26-05:00 List-Id: "Jere" wrote in message news:6792fcd7-a25a-417c-b45a-1a17b0168234@googlegroups.com... > On Friday, March 9, 2018 at 11:46:03 AM UTC-5, Jeffrey R. Carter wrote: >> On 03/09/2018 01:36 AM, Jere wrote: >> > >> > It generates the error "access-to-variable designates constant" because >> > the function takes a parameter of mode "in" while the iterator needs >> > a non-constant access to the variable. I can't change the mode of the >> > function for other reasons. >> >> OK, that's what I missed. I guess you could unchecked convert from >> access-constant to access-all. >> > My only concern with this is does the Ada language allow it portably? Jeff's answer is essentially correct: it ought to be OK for by-reference types -- in particular, see RM 3.3(13/3) which notes that controlled and immutably limited types are never really constant. For other types, however, the compiler is allowed to assume that a constant is not actually going to change, and that could mean ignoring any chances in subsequent code. One easy way to get a rewritable reference of a controlled type is to save it in the Initialize routine (note that the argument of Initialize is "in out"). So you could have something like: type Container is tagged; type Acc_Container is access all Container; type Container is new Controlled with record Self : Acc_Container; end record; procedure Initialize (Obj : in out Container) is begin Obj.Self := Obj'Access; end Initialize; And then you can use "Self" anytime you need to write the container object. Claw does this extensively (we didn't invent it for this reason, but we certainly used it this way). If you have an immutably limited type, you can do this directly in the type declaration; it's known as the "Rosen technique" (or "trick" if you dislike it ;-). It's commonly used, and the reason we rewrote 3.3(13/3) for Ada 2012 was to clarify that it was legitimate (it would be unfortunate for a widely-used technique that every Ada compiler needs to support to be formally erroneous). Randy.