comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: specification file ads problem
Date: Mon, 6 Oct 2014 14:34:16 -0700 (PDT)
Date: 2014-10-06T14:34:16-07:00	[thread overview]
Message-ID: <f7dc0c7e-ba35-417f-be6a-c38e597128df@googlegroups.com> (raw)
In-Reply-To: <b8309b51-0c1c-4b76-b1ea-b69c0989eb56@googlegroups.com>

On Sunday, October 5, 2014 9:20:35 AM UTC-7, mockturtle wrote:
> I see an easy error in your code, but I am not sure if it is an error introduced by doing cut'n'paste: the spec of your package  "MyPackage" ends with "end Neighbours" rather than with "end MyPackage."
> 
> 
> 
> Moreover, I think that you should put "N : Integer" before the 
> 
> definition of myType since when the compiler sees "type myType is..." does not know what N is.  
> 
> 
> 
> Moreover, I am not 100% sure that you can use N-1 as array boundary.  I know you cannot do that in a record declaration. 

Actually, you *can* do that in a record declaration, as long as "N" isn't a discriminant.  The rules that disallow arithmetic on array bounds in a record component apply only to discriminants.

For a generic formal array type, however, you can't use ranges at all, by RM 12.5.3(3).  The official reason for this, according to the AARM, is "it simplifies the elaboration of generic_declarations (since there is nothing to evaluate), and that it simplifies the matching rules, and makes them more checkable at compile time."  (12.1(7.a))  This rule prohibits both 1..N-1 on the outer array type and 1..N on the String element type.  

I don't see a way to get the compiler to check this at compile time.  Checking at run time is really clunky; this is the only way I could find to get things to work:

generic
  type String_Index is range <>;
  type Array_Index is range <>;
  type String_Subtype is array (String_Index) of Character;
  type myType is array (Array_Index) of String_Subtype;
  N : Integer;
package myPackage is
  function tt (a : in String) return myType;
end myPackage;

package body myPackage is
  -- define tt 

begin
  if String_Index'first /= 1 or else
     Integer(String_Index'last) /= N or else
     Array_Index'first /= 1 or else
     Integer(Array_Index'last) /= N - 1 then
     raise Program_Error with "Instantiation with wrong types";
  end if;
end myPackage;

and to instantiate it, you need something like

    subtype Integer_3 is integer range 1..3;
    subtype String3 is String(Integer_3);
    subtype Integer_2 is integer range 1..2;
    type Arr is array(Integer_2) of String3;
    package myPack is new myPackage(Integer_3, Integer_2, String3, Arr, 3);

When the instantiation is elaborated, the package body is elaborated, which causes the code in the "begin..end" of the package body to be executed, which raises an exception at run time if the array bounds are wrong.

I think the design probably needs reworking; to start with, it isn't clear to me that a generic is needed at all.  But it's hard to tell without more information about what the OP is actually trying to accomplish.

                                   -- Adam

  reply	other threads:[~2014-10-06 21:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-05 15:39 specification file ads problem Stribor40
2014-10-05 16:20 ` mockturtle
2014-10-06 21:34   ` Adam Beneschan [this message]
2014-10-05 16:29 ` AdaMagica
2014-10-05 16:32 ` Simon Wright
2014-10-05 17:06   ` Online conceptual/tutorial material for Ada, was: " Simon Clubley
2014-10-05 18:44     ` Niklas Holsti
2014-10-05 19:53     ` Peter Chapin
2014-10-06  7:55       ` Simon Wright
2014-10-06 11:08         ` Peter Chapin
2014-10-06 12:13           ` G.B.
2014-10-06 13:07             ` Peter Chapin
2014-10-06 13:40               ` J-P. Rosen
2014-10-06 22:30             ` Simon Clubley
2014-10-07 18:39               ` Peter Chapin
2014-10-06  8:12     ` Jacob Sparre Andersen
replies disabled

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