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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,5c1c45943bf6a5bc X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: 'first of strings returned from a function should be 1? Date: 1997/07/27 Message-ID: #1/1 X-Deja-AN: 259479976 Distribution: world References: <5rcaqi$le8$1@goanna.cs.rmit.edu.au> <01bc9a76$459c2250$4c8371a5@dhoossr> Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-07-27T00:00:00+00:00 List-Id: In article <01bc9a76$459c2250$4c8371a5@dhoossr>, "David C. Hoos, Sr." wrote: >>From my own experience, I know that it is easy to write a poor function >returning a string result of which the first subscript is not 1, Poor is a relative term. For some abstractions, returning a string whose lower index is not 1 might make more sense. In the example I gave, function To_Uppercase (S : String) return String; one could argue that the return value should have the same bounds as the input string S (which doesn't necessarily have 1 as the lower bound). Consider a sequence: generic type Sequence_Item is private; type Sequence_Item_Array is array (Positive range <>) of Sequence_Item; package Sequences_G is type Root_Sequence is abstract tagged private; function Subsequence (Sequence : Root_Sequence; First : Positive; Last : Positive) return Sequence_Item_Array; Surely, the array returned by this function would have First, not 1, as its lower bound. >and I >always make the effort to slice (if necessary) the result into the range 1 >. result'length, but if you're using >something you can't change, then something like >New_Result (1 .. Result_String'length) := >Result_String(Result_String'first .. result_String'first + >Result_String'length -1) >will do the job. Why not Result_String'Last, instead of Result_String'First + Result_String'Length - 1 ? And why not := Result_String instead of := Result_String (Result_STring'First .. Result_String'Last) ? Indeed, why bother with the copy at all: declare Result_String : constant String := f (...); subtype Slid is String (1 .. Result_String'Length); begin if Slid (Result_String)(1) then Even if you do decide to make a copy, why not do it in the declarative region: declare Result_String_Unslid : constant String := f (...); subtype Slid is String (1 .. Result_String_Unslid'Length); Result_String : constant String := Slid (Result_String_Unslid); begin if Result_String (1) ... -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271