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,FREEMAIL_FROM, 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: johnherro@aol.com Subject: Re: [Q] Returning Strings From A Function Date: 1997/04/05 Message-ID: <19970405130401.IAA25121@ladder01.news.aol.com>#1/1 X-Deja-AN: 230903762 References: <33454165.1658515@news.demon.co.uk> X-Admin: news@aol.com Organization: AOL http://www.aol.com Newsgroups: comp.lang.ada Date: 1997-04-05T00:00:00+00:00 List-Id: (John McCabe) writes: > [In Ada 83] I have a function ... which returns a string. > If I have an object, let's say File_Name, what would be > the correct (or best) way to declare that object in order > for the statement: > > File_Name := Text_IO.Name (File); > > ... to work without me getting a constraint error by trying > to assign a string of n characters to an object /=n > characters long? This has always been a minor annoyance with Ada 83. Of course it's not a problem with Ada 95's unbounded strings. Obviously you can't use direct assignment if the strings have different lengths. One solution is to use the package Text_Handler recommended in Sec. 7.6 of the Ada 83 LRM. The LRM supplies only the package specification, but writing the body is only about an hour's work. (Some Ada 83 compilers, such as Open Ada, come with the Text_Handler package ready to use.) A similar, but simpler, solution is simply to declare *two* variables, File_Name and File_Name_Len. You can write a simple procedure to assign to them together: procedure Set(Target: out String; Target_Len: out Natural; Source: in String) is begin Target_Len := Source'Length; Target(1 .. Source'Length) := Source; end Set; In the second executable line above, you can't replace Source'Length with Target_Len (in Ada 83) unless you make Target_Len an "in out" parameter. Of course, the call to the procedure would be Set(File_Name, File_Name_Len, Text_IO.Name(File)); This call isn't particularly easy to read, so named parameter association is definitely an option to consider here. Does anyone have other solutions? - John Herro Software Innovations Technology http://members.aol.com/AdaTutor ftp://members.aol.com/AdaTutor