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-7-bit Path: g2news1.google.com!news4.google.com!newsfeed2.dallas1.level3.net!news.level3.com!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Surprise in array concatenation Date: 01 Sep 2005 11:57:57 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <1125544603.561847.32140@g47g2000cwa.googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1125590278 28277 192.74.137.71 (1 Sep 2005 15:57:57 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 1 Sep 2005 15:57:57 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: g2news1.google.com comp.lang.ada:4375 Date: 2005-09-01T11:57:57-04:00 List-Id: "Gene" writes: > Seldom am I surprised by Ada semantics, but today they got me. > > type Int_List_Type is array(Integer range <>) of Integer; "Positive range <>" or "Natural range <>" is probably a better idea. If you have an unconstrained array such that Index_Type'First = Index_Type'Base'First, you will run into trouble with empty arrays. > return F(a(1..i - 1) & a(i + 1, a'Last)); > 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. Consider: X: String(-99 .. -10_000); -- empty string Y: constant String := X & "Hello world." Ada requires the bounds of a String to be in Positive, EXCEPT when it's an empty String. So X'First = -99, but Y'First can't be -99. Your suggestion would make sense if Ada didn't allow weird things like String(-99 .. -10_000). It shouldn't -- allowing such things just causes confusion and bugs. The rule should be that 'First of every String is exactly 1. The 'Last should be in Positive, except in the empty string case, where the bounds should be exactly 1..0. The syntax for declaring String could be: type String(Length: Natural) is array (Positive range 1..Length) of Character; No need for the "range <>" syntax in this language! There's also an efficiency advantage: the dope can be just one word instead of two, and the code to calculate 'Length could be branch free. Note that Ada 200X has a Vectors generic container package, which implements growable arrays. If you pass in Integer as the index type, you will get Constraint_Error at instantiation time, for this very reason. For the same reason, you can't pass an enumeration or modular type as the index type -- empty vectors wouldn't work. > 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? - Bob