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,5f5a48f21d7f7525 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!news.glorb.com!news2.glorb.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Inferring array index type from array object Date: Wed, 23 Jun 2010 15:13:18 +0300 Organization: Tidorum Ltd Message-ID: <88ec2vF3uqU1@mid.individual.net> References: <6b20ed09-efc1-4df7-90f9-5e141482e8d0@d37g2000yqm.googlegroups.com> <1305oqccr1h2t$.x33x4oxwd84d$.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 2ZHu0UWDJVFGEbvFDdkyJAO0Pi/7A3D5G7s3GgDekv9sgcExZj Cancel-Lock: sha1:ZR52dJbHcdxu4dqpvj4qm5jEDKw= User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) In-Reply-To: <1305oqccr1h2t$.x33x4oxwd84d$.dlg@40tude.net> Xref: g2news1.google.com comp.lang.ada:11884 Date: 2010-06-23T15:13:18+03:00 List-Id: Dmitry A. Kazakov wrote: > On Wed, 23 Jun 2010 00:30:23 -0700 (PDT), Maciej Sobczak wrote: > >> Consider this: >> >> S : String := "Hello"; >> >> It is possible to iterate over all indices of this array with this >> construct: >> >> for I in S'Range loop ... >> >> 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; > >> I have two questions: >> >> 1. What is the standard justification for this assymetry? > > "We don't want to do anything [useful]." (:-)) > >> What exactly >> makes S'Range "work" in a for loop? > > It is hard wired, since ranges are not first class citizens. > >> 2. Is it possible to declare the index variable without hardcoding the >> index type (that is, to infer it from the array object)? > > No, without improving the type system. E.g. introducing abstract index > types, and abstract range types (or more general sets of index types), and > abstract composite types like arrays. I don't think such large language changes would be necessary. The expression S'Range gives the compiler all the information about the type and its constraints, so I see no reason why Ada could not be extended simply to allow S'Range in a variable declaration, as in the above quoted "I : S'Range". The declared variable "I" would have the same (sub)type as the loop counter in "for I in S'Range loop ...". On the other hand, I haven't really felt the need for this extension in my programs. Whenever I declare an array type, either the index type has no meaning for the application, and I use a subtype of Integer (eg. the array just represents a sequence or set of objects), or I also define an index type (eg. an object identifier) that is associated with the array type. So I have not felt a need for using S'Range in a variabnle declaration. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .