comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: [Q] Returning Strings From A Function
Date: 1997/04/07
Date: 1997-04-07T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680000704972129280001@news.ni.net> (raw)
In-Reply-To: 01bc43b8$a4c441a0$09f682c1@xhv46.dial.pipex.com


In article <01bc43b8$a4c441a0$09f682c1@xhv46.dial.pipex.com>, "Nick
Roberts" <Nick.Roberts@dial.pipex.com> wrote:


>Argh! The embarrassment! Sudden brainstorm, Bob (my excuse being that I am
>using a lot of different languages at the moment (pretty feeble, eh?)).
>Anyway, what I _should_ have written was this...
>
>
>declare
>   F: Text_IO.File_Type;
>   ...
>   type Filename_String is array (1..12 range <>) of Character;
>   type Filename_Ref is access Filename_String;
>   ...
>   S: Filename_Ref;
>   ...
>begin
>   ...
>   S := new Filename_String'(Text_IO.Name(F)); -- constraint error raised
>if name longer than 12 chars
>   ...
>   Text_IO.Put("Name of file is: "+String(S.all)); -- note the need to
>dereference and convert
>   ...
>end;

Nick: 

This is an equally confusing solution.  No heap is required:

declare
   The_Name : String (1 .. 12);
begin
   The_Name := Text_IO.Name (F);
   -- This assignment will raise Constraint_Error if the length of the 
   -- return value is not _exactly_ 12.  (It could be lesser or greater.)

   Text_IO.Put ("Name is " & The_Name);
exception
   when Constraint_Error => ...;
end;

Two issues:

1.  Do not use heap when the stack will do.

2. Don't create new string types, ie use subtypes of Standard.String unless
you have a compelling need not to.  In this case, you wanted a string of
length 12, so just use Standard.String:

The_Name : String (1 .. 12);  -- an "anonymous" subtype

subtype Name_String is String (1 .. 12);
The_Name : Name_String;  -- a named subtype

KISS is the operative word.

Matt

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




  reply	other threads:[~1997-04-07  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 ` 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       ` 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 [this message]
1997-04-06  0:00       ` Matthew Heaney
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
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-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