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: 103376,db88d0444fafe8eb X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news2.google.com!proxad.net!134.158.69.22.MISMATCH!in2p3.fr!oleane.net!oleane!hunter.axlog.fr!nobody From: Jean-Pierre Rosen Newsgroups: comp.lang.ada Subject: Re: Surprise in array concatenation Date: Thu, 01 Sep 2005 10:48:19 +0200 Organization: Adalog Message-ID: References: <1125544603.561847.32140@g47g2000cwa.googlegroups.com> NNTP-Posting-Host: mailhost.axlog.fr Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: s1.news.oleane.net 1125565270 24728 195.25.228.57 (1 Sep 2005 09:01:10 GMT) X-Complaints-To: abuse@oleane.net NNTP-Posting-Date: Thu, 1 Sep 2005 09:01:10 +0000 (UTC) User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: fr, en In-Reply-To: <1125544603.561847.32140@g47g2000cwa.googlegroups.com> Xref: g2news1.google.com comp.lang.ada:4361 Date: 2005-09-01T10:48:19+02:00 List-Id: Gene a �crit : > Seldom am I surprised by Ada semantics, but today they got me. > > type Int_List_Type is array(Integer range <>) of Integer; > > procedure Main is > > function F(a : Int_List_Type) return Integer is > i : Integer; > begin > if a'Length <= 1 then > return a(1); > else > -- calculate i s.t. 1 <= i <= a'Last > -- with some algorithm; details irrelevant > ... > return F(a(1..i - 1) & a(i + 1, a'Last)); > end if; > end Calculate; > > a : Int_List(1..100); > > begin > -- yada yada; fill a with values > Put(Calculate(F(a))); > end; > > The gotcha is that when (and only when) i is determined to be zero, the > concatenation in the recursive call to F produces a starting index of 2 > rather than 1. Of course this causes the base case test to raise an > exception in the next recursive call. This is in accordance with the > ALRM and GNAT implements it perfectly. > > Yes I know how to recode this to use a'First instead of 1 as needed. > > Nonetheless the rule seems silly: When a leading zero-length array is > catenated to another array the result takes on the starting index of > the _second_ operand. This doesn't make sense to me. Why not use the > starting index of the zero-length array? > Because the lower bound of a null array needs not belong to the subtype used for indexing. Note that you can force the lower bound by subtype conversion: declare Result : constant string := F(a(1..i - 1) & a(i + 1, a'Last)); subtype Force_Lower_To_1 is string (1 .. Result'length); begin return Force_Lower_To_1 (Result); end; -- --------------------------------------------------------- J-P. Rosen (rosen@adalog.fr) Visit Adalog's web site at http://www.adalog.fr