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,start 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/26 Message-ID: #1/1 X-Deja-AN: 259207391 Distribution: world References: <5rcaqi$le8$1@goanna.cs.rmit.edu.au> Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-07-26T00:00:00+00:00 List-Id: In article <5rcaqi$le8$1@goanna.cs.rmit.edu.au>, Dale Stanbrough 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 ; 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 ; 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 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 (818) 985-1271