comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Substrings as argument to procedures/functions
Date: Tue, 12 Apr 2016 20:37:11 +0200
Date: 2016-04-12T20:37:11+02:00	[thread overview]
Message-ID: <nejf8s$k0e$1@gioia.aioe.org> (raw)
In-Reply-To: b418f784-0626-49f8-82bb-7dbab07abd62@googlegroups.com

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

  reply	other threads:[~2016-04-12 18:37 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-12  8:25 Substrings as argument to procedures/functions reinkor
2016-04-12  9:01 ` Dmitry A. Kazakov
2016-04-12 16:55   ` reinkor
2016-04-12 18:37     ` Dmitry A. Kazakov [this message]
2016-04-13  4:44       ` reinkor
2016-04-13  7:19         ` Dmitry A. Kazakov
2016-04-13 10:16         ` Brian Drummond
2016-04-13 11:43           ` reinkor
2016-04-13 11:54             ` Pascal Obry
2016-04-13 12:30               ` Mart van de Wege
2016-04-13 12:47                 ` Egil H H
2016-04-13 13:19                   ` Mart van de Wege
2016-04-13 16:57                 ` Jeffrey R. Carter
2016-04-12 18:17 ` Robert A Duff
2016-04-12 18:34 ` Jeffrey R. Carter
2016-04-13 21:29   ` Randy Brukardt
2016-04-25 15:33     ` rieachus
2016-04-25 22:07       ` Randy Brukardt
2016-04-26  6:12         ` Georg Bauhaus
2016-04-26 18:41           ` Randy Brukardt
2016-04-13 11:22 ` G.B.
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox