comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: renaming function result object inside loop
Date: Sun, 06 Sep 2009 12:03:58 -0400
Date: 2009-09-06T12:03:58-04:00	[thread overview]
Message-ID: <wccd464givl.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 4aa2a2ed$0$32665$9b4e6d93@newsspool2.arcor-online.net

Georg Bauhaus <rm.tsoh.plus-bug.bauhaus@maps.futureapps.de> 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



  parent reply	other threads:[~2009-09-06 16:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-05 17:42 renaming function result object inside loop Georg Bauhaus
     [not found] ` <gpidncZaHuLEXT_XnZ2dnUVZ_j-dnZ2d@earthlink.com>
2009-09-05 20:27   ` Georg Bauhaus
2009-09-06 11:06 ` Oliver Kellogg
2009-09-06 16:03 ` Robert A Duff [this message]
2009-09-06 20:09   ` Georg Bauhaus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox