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,7b6305d0d57a9f34 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Received: by 10.204.146.17 with SMTP id f17mr545448bkv.5.1319834359578; Fri, 28 Oct 2011 13:39:19 -0700 (PDT) Path: l23ni8544bkv.0!nntp.google.com!news2.google.com!postnews.google.com!u24g2000pru.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Normalizing array indices Date: Fri, 28 Oct 2011 13:36:19 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 X-Trace: posting.google.com 1319834267 22993 127.0.0.1 (28 Oct 2011 20:37:47 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 28 Oct 2011 20:37:47 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: u24g2000pru.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: ARLUEHNKC X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C),gzip(gfe) Xref: news2.google.com comp.lang.ada:14226 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2011-10-28T13:36:19-07:00 List-Id: On Oct 28, 11:58=A0am, Stefan.Lu...@uni-weimar.de wrote: > Hi all, does anyone know a way to change the array indices of a subprogra= m > parameter to start with a default index? This question occurred to me whe= n > I happened to discover a subtle bug in a sort procedure I had implemented= . > > =A0 generic > =A0 =A0 type Element_Type is private; > =A0 =A0 type Sort_Array_Type is array (Positive range <>) of Element_Type= ; > =A0 =A0 with function "<" (Left, Right: Element_Type) return Boolean is <= >; > =A0 procedure Sort(A: in out Sort_Array_Type); > > I had a reasonable amount of black box tests and Sort passed all of them. > > Some time later, I added a test with A'range being =A0 > =A0 =A0Positive'Last -2 .. Positive'Last > and boooom -- got a Constraint_Error. As it turned out, there was a > Positive index variable which could take the value A'Last+1 -- which is > perfectly OK except when A'Last =3D Positive'Last. To rescue my > implementation I considered something like > > =A0 procedure Sort(A: in out Sort_Array_Type) is > =A0 =A0 Alias_A: Sort_Array_Type(1 .. A'Length) renames A; > =A0 begin > =A0 =A0 ... -- apply your favorite sorting algorithm to Alias_A; > =A0 end Sort; > > but the compiler didn't like that renaming: > =A0 "constraint not allowed in object renaming declaration". procedure Sort (A : in out Sort_Array_Type) is subtype A_Type is Sort_Array_Type (1 .. A'Length); Alias_A : A_Type renames A_Type(A); begin ... end Sort; The type conversion is necessary in order to get the indexes to "slide". If you do this: Alias_A : A_Type renames A; the compiler will accept it, but you're likely to get Constraint_Errors at runtime. -- Adam