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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Substrings as argument to procedures/functions Date: Tue, 12 Apr 2016 20:37:11 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <2055a188-fb5f-496a-ab37-b25d81cebe1b@googlegroups.com> NNTP-Posting-Host: LMk7+sG0YqgPmReI4fVkAA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:30086 Date: 2016-04-12T20:37:11+02:00 List-Id: On 2016-04-12 18:55, reinkor wrote: > On Tuesday, April 12, 2016 at 11:02:03 AM UTC+2, Dmitry A. Kazakov wrote: >> On 12/04/2016 10:25, reinkor wrote: >> >>> I believed that if a substring is an argument to a subroutine, >>> then its "range" would start at 1 (and not inherited from the calling program). >>> But this seems not to be the case. >>> >>> Given the following Ada source code: >>> >>> with Text_IO; >>> use Text_IO; >>> procedure t3 is >>> package Int_Io is new Text_IO.Integer_Io (Integer); >>> use Int_Io; >>> procedure string_test1(S : String) is >>> begin >>> New_Line; >>> Put(" In string_test1: "); >>> Put(S'First,4); >>> Put(S'Last,4); >>> end string_test1; >>> Str : String := "ABCDEF"; >>> begin >>> string_test1(Str); >>> string_test1(Str(3..5)); >>> end t3; >>> >>> The second call to "string_test1" gives: >>> >>> In string_test1: 3 5 >>> >>> (at least on my computer). >>> >>> Is this correct? And in case, is it "good" ? :-) >> >> Yes, it is good. >> >> Consider a program that uses an index to walk through a string/array. >> When you pass a substring of the string and an index into it (usually >> in-out) down to a subprogram, then the index remains valid. > > But as far as I remember, several other program languages do not > bring the initial indexing down to subroutines like this? I don't remember any other language implementing arrays in a consistent way. > Anyway, one may for example copy strings in soubroutines if one really want to start indexing at one. Why would you want it to start at a certain number? There is S'First that tells where it begins. In Ada 2012 you can constrain a string subtype to always begin at 1, which is not a great idea. Though it will crash your program sooner than otherwise. The rule is that the preconditions must be as weak as possible. If you require the index to begin at 1 you strengthen the subprogram's precondition. If you don't require it, but silently slide indices, you have two different types outside the call and inside it. This is even worse, because it breaks the substitutablity rule in so many cases. I think that one problem is confusing index with position. Index and string bounds are absolute, they are not positions or offsets. It would be nice if the language separated them more cleanly. Note that it does this in some cases. For example S'Length is not index, it is a position. So its type is not the type of the index, e.g. Stream_Element_Offset for Stream_Element_Array, but universal integer. The type of index is a specific type. The type of position is universal integer. The first position is always 1. The first index is any. > Sometimes I want the soubroutines not to "know" too much :-) Knowing that the index type and the string type bound to the same object remain consistent with the slicing operation is not "too much", it is knowing that the language design is sane. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de