From: Jeff Carter <carter@spam.innocon.com>
Subject: Re: [Q] Returning Strings From A Function
Date: 1997/04/08
Date: 1997-04-08T00:00:00+00:00 [thread overview]
Message-ID: <334A4ADE.41C67EA6@spam.innocon.com> (raw)
In-Reply-To: 33474626.4BA3@us.net
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
next prev parent reply other threads:[~1997-04-08 0:00 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-04-04 0:00 [Q] Returning Strings From A Function John McCabe
1997-04-04 0:00 ` Joakim Olsson
1997-04-05 0:00 ` johnherro
1997-04-05 0:00 ` Mark & Zurima McKinney
1997-04-07 0:00 ` johnherro
1997-04-07 0:00 ` Robert Dewar
1997-04-07 0:00 ` Jon S Anthony
1997-04-07 0:00 ` johnherro
1997-04-08 0:00 ` Jeff Carter [this message]
1997-04-09 0:00 ` Looking for an Ada SCIENTIFIC UNITS checking package Ron House
1997-04-05 0:00 ` [Q] Returning Strings From A Function Robert Dewar
1997-04-06 0:00 ` John McCabe
1997-04-06 0:00 ` Robert Dewar
1997-04-05 0:00 ` John McCabe
1997-04-05 0:00 ` Robert A Duff
1997-04-05 0:00 ` Robert Dewar
1997-04-06 0:00 ` John McCabe
1997-04-06 0:00 ` Matthew Heaney
1997-04-06 0:00 ` Robert Dewar
1997-04-06 0:00 ` Nick Roberts
1997-04-07 0:00 ` Robert A Duff
1997-04-08 0:00 ` Nick Roberts
1997-04-07 0:00 ` Matthew Heaney
1997-04-06 0:00 ` John McCabe
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox