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: mheaney@ni.net (Matthew Heaney) Subject: Re: [Q] Returning Strings From A Function Date: 1997/04/07 Message-ID: #1/1 X-Deja-AN: 231461109 References: <33454165.1658515@news.demon.co.uk> <33478738.2129057@news.demon.co.uk> <01bc42af$e32e3ea0$90f482c1@xhv46.dial.pipex.com> <01bc43b8$a4c441a0$09f682c1@xhv46.dial.pipex.com> Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-04-07T00:00:00+00:00 List-Id: In article <01bc43b8$a4c441a0$09f682c1@xhv46.dial.pipex.com>, "Nick Roberts" 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 (818) 985-1271