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=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,3ebfb7ec7bfb06fa X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit X-Received: by 10.224.72.199 with SMTP id n7mr13298330qaj.5.1360069267122; Tue, 05 Feb 2013 05:01:07 -0800 (PST) Path: k2ni8715qap.0!nntp.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!news.flashnewsgroups.com-b7.4zTQh5tI3A!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: Passing indefinite types References: <5262a822-409a-4c79-a842-0e716527cb70@googlegroups.com> <85ip696nlk.fsf@stephe-leake.org> <8d39b2ad-dc6c-4492-bc43-d2d01d748efa@googlegroups.com> Date: Tue, 05 Feb 2013 08:01:04 -0500 Message-ID: <85r4ku52zz.fsf@stephe-leake.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (windows-nt) Cancel-Lock: sha1:LhbrTgH1rqsN7ynlzPteY20fjLU= MIME-Version: 1.0 X-Complaints-To: abuse@flashnewsgroups.com Organization: FlashNewsgroups.com X-Trace: 0c41151110293c55ab71402590 X-Received-Bytes: 4784 Content-Type: text/plain Date: 2013-02-05T08:01:04-05:00 List-Id: sbelmont700@gmail.com writes: > On Sunday, February 3, 2013 5:26:15 PM UTC-5, Stephen Leake wrote: >> >> Could you show some almost legal code? >> > > package k > type p is access all string; > type a is array (positive range <>) of p; > function find (p: string, sources : a) return boolean; > end k; > > But you can't ever call this from a subprogram with locally declared > strings, since trying to create p.a with local strings fails the > accessibility check. Just to be complete: package k is type p is access all string; type a is array (positive range <>) of p; function find (p: string; sources : a) return boolean; end k; with Ada.Text_IO; use Ada.Text_IO; with K; procedure Try_K is String_1 : aliased String := "Hello"; String_2 : aliased String := "There"; Sources : constant K.A := (String_1'Access, String_2'Access); begin Put_Line ("Find => " & Boolean'Image (K.Find ("There", Sources)) ); end Try_K; try_k.adb:7:31: non-local pointer cannot point to local object try_k.adb:7:48: non-local pointer cannot point to local object >You can use heap values or unchecked_access, but that's ugly and ought >to be unecessary, since the subprogram is not doing anything sketchy >with the values. So you are looking for a way to have a collection of ragged, statically declared strings. In C you can do this, with essentially the same code as above, but the C compiler doesn't complain about accessibility. In Ada you can do this with a container, but that does involve the heap. I think the standard response here is "worrying about the heap is premature optimization". Unless of course you are using Ravenscar or some other target, that doesn't have a heap :). > This is the most aggrevating, because p (in this case) is not actually > indefinite, and is always a single, fixed-size In that case, you _can_ have an array of static strings: package k2 is subtype p is String (1 .. 5); type a is array (positive range <>) of p; function find (p: string; sources : a) return boolean; end k2; package k2 is subtype p is String (1 .. 5); type a is array (positive range <>) of p; function find (p: string; sources : a) return boolean; end k2; package body k2 is function find (p: string; sources : a) return boolean is begin for I of Sources loop if P = I then return True; end if; end loop; return False; end find; end k2; ./try_k2.exe Find => TRUE If you need different values for p'last, use a generic. >> I wonder if it would be possible to write an aspect or other constraint >> that asserts "no pointers are copied"? > That's what access discriminats (and to a lesser extent, access > parameters) usually work well for, but I suppose there is no way to > create an unconstrained number of them. What I would like to see is an > aspect that says "this discriminated record is not going to be > variant, so it's okay to nest it within records and arrays", so that > arrays of 'accessors' are legal. You are missing my point. The reason the compiler doesn't like the first solution above is that it assumes 'find' might copy the pointers in 'sources'. So if you can add an aspect to find: function find (p: string; sources : a) return boolean with Does_Not_Copy_Access_Values => True; Then the compiler could eliminate the accessibility check, and the first approach would be legal. GNAT 7.1.0w doesn't like this, because Does_Not_Copy_Access_Values is not a predefined aspect. So this is the beginnings of a proposed addition to Ada 202x. I tried using Suppress: pragma Suppress (Accessibility_Check, String_1); pragma Suppress (Accessibility_Check, Sources); pragma Suppress (Accessibility_Check, A); None of those helped; I'm not clear why not. Possibly because the failure is a legality issue, not a run-time check issue. -- -- Stephe