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,5f5a48f21d7f7525 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news1.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: Inferring array index type from array object Date: Wed, 23 Jun 2010 10:38:45 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <6b20ed09-efc1-4df7-90f9-5e141482e8d0@d37g2000yqm.googlegroups.com> <1305oqccr1h2t$.x33x4oxwd84d$.dlg@40tude.net> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: pcls6.std.com 1277303908 32368 192.74.137.71 (23 Jun 2010 14:38:28 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Wed, 23 Jun 2010 14:38:28 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:hab5xR4jtaSbQXTw+8E8Hx1WT5o= Xref: g2news2.google.com comp.lang.ada:12860 Date: 2010-06-23T10:38:45-04:00 List-Id: "J-P. Rosen" writes: > Dmitry A. Kazakov a �crit : >>> I would like to declare I as a free variable instead and I would >>> expect some symmetry in the language by doing this: >>> >>> I : S'Range := S'First; >> >> subtype Index_Span is Integer range S'Range; >> I : Index_Span := S'First; > Or simply: > I : Positive range S'Range; Right. And as others have noted, beware null ranges. >>> 1. What is the standard justification for this assymetry? > This is a simplification of loops, to avoid some typing to the user. > Think of it the other way round: since a for loop involves the > declaration of an object, the type should always be explicitely declared > (a coding rule that I apply - of course checkable by AdaControl): > > for I in positive range 1..10 loop ... > for I in positive range S'Range loop ... I see those two lines as very different. I agree with you on the first line. In "for I in 1..10", Ada takes a "wild guess", and says the type is Integer. That's a language design mistake, and I always like to make the type explicit, as shown above. Well, almost always -- I might leave it implicit if I is never mentioned, as in: -- Print "Hello" ten times: for I in 1..10 loop Put_Line ("Hello"); end loop; But in the second example, I'm perfectly happy with leaving the type implicit: for I in S'Range loop ... S(I) ... because this code really doesn't care what the index type is. We're saying "the type of I is whatever the index type of S is, and we don't need to name it here". Can AdaControl check my style rule -- complain if I depend on the default-to-integer rule, but not complain if the type of the loop index is properly deducible from the stuff between "in" and "loop"? Note that Ada 2012 will (probably) eliminate the need for I altogether, which is a good thing, since I is just an extraneous thing -- we really want to talk about the components of S here, not their index values. > So it is really symetrical, except that in the case of a loop, you are > allowed to "simplify" by omitting the " range" part - forcing the > compiler to deduce the type from the range, which is not a good idea IMHO. Why is that a bad idea (in the second case)? - Bob