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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,c4d1943c804f7cdd X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!newspeer.monmouth.com!newsswitch.lcs.mit.edu!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: renaming function result object inside loop Date: Sun, 06 Sep 2009 12:03:58 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <4aa2a2ed$0$32665$9b4e6d93@newsspool2.arcor-online.net> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls6.std.com 1252253038 3018 192.74.137.71 (6 Sep 2009 16:03:58 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Sun, 6 Sep 2009 16:03:58 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:8U6ZqWejPqOGwP32my/xTvYOFSM= Xref: g2news2.google.com comp.lang.ada:8196 Date: 2009-09-06T12:03:58-04:00 List-Id: Georg Bauhaus writes: > Is the following a spurious warning? > > 528. loop > 529. declare > 530. Line : String renames LIO.Get_Line; --! > | > >>> warning: renaming function result object is suspicious > >>> warning: function "Get_Line" will be called only once > >>> warning: suggest using an initialized constant object instead > > (Get_Line in fact seems to be called each time around the loop.) The history of this warning is that some folks were confused between renaming a function versus renaming the result of calling the function, and AdaCore thought this warning would help. Line : String renames LIO.Get_Line; vs. function Line return String renames LIO.Get_Line; In the first case, Get_Line is called "once" (i.e. when the declaration of Line is elaborated), whereas in the second case, Get_Line is called every time you say "Line". Since a constant: Line : constant String := LIO.Get_Line; has the same semantics as the first renaming, it seems preferable (and if you're lucky, the compiler will generate the same code). Of course, in the above example, "once" really means "once each time through the loop", that is, "many times", which seems confusing in a different way. The renaming is necessary in Ada 95 for limited types, but in Ada 2005, you can even use the constant form for limited types. - Bob