comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: Passing indefinite types
Date: Tue, 05 Feb 2013 08:01:04 -0500
Date: 2013-02-05T08:01:04-05:00	[thread overview]
Message-ID: <85r4ku52zz.fsf@stephe-leake.org> (raw)
In-Reply-To: 8d39b2ad-dc6c-4492-bc43-d2d01d748efa@googlegroups.com

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



  parent reply	other threads:[~2013-02-05 13:01 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-02 21:07 Passing indefinite types sbelmont700
2013-02-02 21:31 ` Jeffrey Carter
2013-02-03  0:27   ` sbelmont700
2013-02-03  8:07     ` Dmitry A. Kazakov
2013-02-03 10:22 ` gautier_niouzes
2013-02-03 22:26 ` Stephen Leake
2013-02-04  1:25   ` sbelmont700
2013-02-04  3:46     ` Shark8
2013-02-05  2:40     ` Randy Brukardt
2013-02-05 13:01     ` Stephen Leake [this message]
2013-02-05 20:56       ` Randy Brukardt
2013-02-05 21:16 ` sbelmont700
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox