comp.lang.ada
 help / color / mirror / Atom feed
* renaming function result object inside loop
@ 2009-09-05 17:42 Georg Bauhaus
       [not found] ` <gpidncZaHuLEXT_XnZ2dnUVZ_j-dnZ2d@earthlink.com>
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Georg Bauhaus @ 2009-09-05 17:42 UTC (permalink / raw)


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.)



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: renaming function result object inside loop
       [not found] ` <gpidncZaHuLEXT_XnZ2dnUVZ_j-dnZ2d@earthlink.com>
@ 2009-09-05 20:27   ` Georg Bauhaus
  0 siblings, 0 replies; 5+ messages in thread
From: Georg Bauhaus @ 2009-09-05 20:27 UTC (permalink / raw)


Dennis Lee Bieber wrote:
> As far
> as I know, each declare block creates a new scope level. Showing more of
> the code (like the portion of the loop using "Line") would be useful
> (especially WRT the first warning)...

The code is that of the K-Nucleotide program.  The corresponding
thread has just got URLs for them, if you like.  The line 530
is from package body Data_Input.  LIO is an instance of Line_IO.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: renaming function result object inside loop
  2009-09-05 17:42 renaming function result object inside loop Georg Bauhaus
       [not found] ` <gpidncZaHuLEXT_XnZ2dnUVZ_j-dnZ2d@earthlink.com>
@ 2009-09-06 11:06 ` Oliver Kellogg
  2009-09-06 16:03 ` Robert A Duff
  2 siblings, 0 replies; 5+ messages in thread
From: Oliver Kellogg @ 2009-09-06 11:06 UTC (permalink / raw)


On Sat, 05 Sep 2009 19:42:05 +0200, Georg Bauhaus wrote:

> 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.)

I like the compiler's advice. IMHO using a constant here is clearer.
For a casual, non-language-lawyer reader of the code, the code may appear 
to either call the function LIO.Get_Line only once in the elaboration of 
the renaming declaration, or at each mention of Line.

Just my EUR 0.02

Oliver Kellogg



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: renaming function result object inside loop
  2009-09-05 17:42 renaming function result object inside loop Georg Bauhaus
       [not found] ` <gpidncZaHuLEXT_XnZ2dnUVZ_j-dnZ2d@earthlink.com>
  2009-09-06 11:06 ` Oliver Kellogg
@ 2009-09-06 16:03 ` Robert A Duff
  2009-09-06 20:09   ` Georg Bauhaus
  2 siblings, 1 reply; 5+ messages in thread
From: Robert A Duff @ 2009-09-06 16:03 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: renaming function result object inside loop
  2009-09-06 16:03 ` Robert A Duff
@ 2009-09-06 20:09   ` Georg Bauhaus
  0 siblings, 0 replies; 5+ messages in thread
From: Georg Bauhaus @ 2009-09-06 20:09 UTC (permalink / raw)


Robert A Duff wrote:
> 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.)
> [...]
> 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).

Thanks for the clarification.  I was in FUD whether
Ada 2005 had changed things in agreement with the warning.
Which I clearly have misunderstood.

A thought about both confusing issues with renaming result
objects then:  If 'Result makes it into the language,
is renaming a function result an opportunity to
allow 'Result?

   Line : String renames LIO.Get_Line'Result;


Anyway,
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41286



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-09-06 20:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2009-09-06 20:09   ` Georg Bauhaus

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