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/26
Date: 1997-07-26T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680002607971748090001@news.ni.net> (raw)
In-Reply-To: 5rcaqi$le8$1@goanna.cs.rmit.edu.au


In article <5rcaqi$le8$1@goanna.cs.rmit.edu.au>, Dale Stanbrough
<dale@goanna.cs.rmit.EDU.AU> wrote:

>Should the 'first of a string returned from a function be 1?

For predefined "functions," such a Integer'Image, the resulting string is
defined to have 1 as its lower index bound.  But for user-defined
functions, it's up to the specifier to define the behavior (and yes, when
you write a spec for a function that returns an unconstrained array, then
you should also specify what the defined bounds are).
>
>(Getting at the first character of such a string requires creating
>a declare block...
>
>        declare
>           result_String : constant String := Some_Function(Params);
>           result_char   : constant Character := Result_String(1);
>        begin
>           ...
>
>rather than just...
>
>    if Some_Function(Params)(1) = ...
>
>if this is the case).

A function invokation (and even a type conversion) can serve as a "name,"
therefore your latter formulation is correct.  So no, it's not true that
you have to use a declare block to get the value at a certain index.

However, your program would be incorrect if the string returned by the
function did not have 1 as its lower index bound; Constraint_Error would
get raised.  That's why, for user-defined functions that return an
unconstrained array (as is type Standard.String), you need to specify what
the behavior is.

For example, if I do this:

function To_Uppercase (S : String) return String;
   -- Returns an uppercase string having the same index bounds as S.

I would implement it as follows:

function To_Uppercase (S : String) return String is

   Return_Value : String (S'Range);
begin
   <make return value be uppercase of S>;
   return Return_Value;
end;

However, I might also do this:

function To_Uppercase (S : String) return String;
   -- Returns an uppercase string having a lower index bound of 1.

and implement it like this:

function To_Uppercase (S : String) return String is
  
   Return_Value : String (1 .. S'Length);
begin
   <make Return_Value be uppercase of S>;
   return Return_Value;
end;

The former solution is easier, in this case, because I don't have to
convert from one index to the other, as they are the same.  The latter
implementation requires that I convert from an index of S to the
corresponding index of Return_Value.

If you have a function whose return value has bounds you're unsure of, you
could slide the return value:

declare
   The_Value : constant String := f (...);
   subtype Slid is String (1 .. The_Value'Length);
begin
   if Slid (The_Value) (1) then ...

That's why I was careful to state that a conversion can be a name, which in
turn can be dereferenced.

But of course, Ada's attributes make even that unnecessary:

declare
   The_Value : constant String := f (...);
begin
   if The_Value (The_Value'First) then ...

Realize that this only applies to functions that return unconstrained
arrays.  For a constrained array subtype, the subtype specifies the index
bounds.

You can combine these ideas, and use an array subtype in the
implementation.  For example, to simplify the implementation of the
To_Uppercase function, do this:

function To_Uppercase (S : String) return String;
  -- Returns an uppercase string having lower bound 1.

function To_Uppercase (S : String) return String is

   Return_Value : String (S'Range);
   
   subtype Slid is String (1 .. S'Length);
begin
   <simpler implementation using identical bounds>

  return Slid (Return_Value);
end;

So the short answer to the question, Do functions in Ada return
unconstrained arrays with a lower bound of 1?,  is no.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




       reply	other threads:[~1997-07-26  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 ` Matthew Heaney [this message]
1997-07-27  0:00 ` 'first of strings returned from a function should be 1? Dale Stanbrough
1997-07-27  0:00 ` David C. Hoos, Sr.
1997-07-27  0:00   ` Matthew Heaney
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
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   ` Simon Wright
replies disabled

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