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,691bbbf0ab0cc67e X-Google-Attributes: gid103376,public From: Jeff Carter Subject: Re: [Q] Returning Strings From A Function Date: 1997/04/08 Message-ID: <334A4ADE.41C67EA6@spam.innocon.com>#1/1 X-Deja-AN: 231542511 References: <33474626.4BA3@us.net> Organization: Innovative Concepts, Inc. Newsgroups: comp.lang.ada Date: 1997-04-08T00:00:00+00:00 List-Id: Guess I ought to put my 2 (cents|pence|centimes|etc :) in. The poster requested information on how to handle the return value from a function that returns type String in Ada 83. Given function F return String; the simplest is I. Use it to initialize a constant S : constant String := F; This is fine if he only needs to store the value. If he also needs to modify the value, things get a little more complicated. The simple answer is II. Do I. and copy the value to a variable Cs : constant String := F; S : String (Cs'range) := Cs; This is often acceptable. Another way is III. Obtain a variable-length string handling package, that provides for conversions to and from String with VSH; ... S : VSH.V_String := VSH.Convert (F); This is sometimes overkill. Intermediate complexity is IV. Write a small function to store a length and value type V_String is record Length : Natural := 0; Value : String (1 .. Max); end record; function Convert (S : String) return V_String is Result : V_String; begin -- Convert Result.Length := S'Length; Result.Value (1 .. S'Length) := S; return Result; end Convert; ... S : V_String := Convert (F); Convert raises Constraint_Error if S won't fit in a V_String. It is also possible for Convert to silently truncate. A common objection to II. is that it "wastes space." Since the strings found in real applications are usually short, this is not a real concern. The poster has since indicated that he's interested in strings <= 12 characters long, so I. or II. should work for him. A common objection to III. is that it's overkill and links in a bunch of excess stuff the program doesn't need. This is also often not a real concern. There are also objections based on NIH or the cost of writing or buying such a package. A good variable-length string package is an essential part of the standard library for an Ada-83 project. Any compentent software engineer can develop one in a few days. PragmAda Software Engineering will sell you one for $25. IV. attempts to overcome the objections to II. and III. This may complicate the software with no true benefit. In Ada (95), one can write S : String := F; and variable-length string packages are part of the language-defined standard library. -- Jeff Carter ( carter @ innocon . com ) "Now go away, or I shall taunt you a second time." Monty Python & the Holy Grail