comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: 'first of strings returned from a function should be 1?
Date: 1997/07/27
Date: 1997-07-27T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680002707971114240001@news.ni.net> (raw)
In-Reply-To: 01bc9a76$459c2250$4c8371a5@dhoossr


In article <01bc9a76$459c2250$4c8371a5@dhoossr>, "David C. Hoos, Sr."
<david.c.hoos.sr@ada95.com> 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
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  parent reply	other threads:[~1997-07-27  0:00 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <5rcaqi$le8$1@goanna.cs.rmit.edu.au>
1997-07-26  0:00 ` 'first of strings returned from a function should be 1? Matthew Heaney
1997-07-27  0:00 ` Dale Stanbrough
1997-07-27  0:00 ` David C. Hoos, Sr.
1997-07-27  0:00   ` Simon Wright
1997-07-27  0:00   ` Dale Stanbrough
1997-07-27  0:00     ` Robert A Duff
1997-07-27  0:00       ` Dale Stanbrough
1997-07-27  0:00         ` Matthew Heaney
1997-07-27  0:00   ` Matthew Heaney [this message]
1997-07-28  0:00     ` Robert A Duff
1997-07-29  0:00       ` Anonymous
1997-07-30  0:00         ` Robert A Duff
1997-07-30  0:00       ` Dale Stanbrough
replies disabled

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