comp.lang.ada
 help / color / mirror / Atom feed
From: Brian Rogoff <bpr@shell5.ba.best.com>
Subject: Re: Return by reference
Date: 1999/11/02
Date: 1999-11-02T00:00:00+00:00	[thread overview]
Message-ID: <Pine.BSF.4.10.9911021905050.1386-100000@shell5.ba.best.com> (raw)
In-Reply-To: wccg0yoyff3.fsf@world.std.com

On Tue, 2 Nov 1999, Robert A Duff wrote:
> Brian Rogoff <bpr@shell5.ba.best.com> writes:
> 
> > I understand why, for a return-by-reference type T, the function 
> > 
> > function Some_Func( ... ) return T is 
> >     Local_Var : T
> > begin
> >     ...
> >     return Local_Var;
> > end Some_Func;
> > 
> > causes Program_Error to be raised at run time, but I'm a bit 
> > perplexed as to why there is a problem for 
> > 
> > function Some_Func( Param : T; ... ) return T is
> > begin
> >     ...
> >     return Param;
> > end Some_Func;
> > 
> > Could someone explain why the latter case is problematic? 
> 
> We wanted the checks to be done at compile time.
> 
> So you may well ask, why does this one raise Program_Error?  It's
> because in a shared-body generic, we don't know enough to do the check
> at compile time.  However, most compilers use macro-expanded generics,
> so they can check the above at compile time, and just generate code to
> raise P_E if the check fails, and, one hopes, give a message at compile
> time.

Thanks, that's what I wanted to know. I should have guessed that the evil 
shared generics were responsible. Any place I can find a further
discussion of this?

> If the above were allowed, then somebody could do this:
> 
>     function Mumble return T is
>         Local: T;
>     begin
>         return Some_Func(Local); -- Wrong.
>     end Mumble;
> 
> To detect that error would require keeping too much dope around at run
> time.

I thought of this case but I expected that it could be caught at compile 
time. 

In any case, in response to Lutz Donnerhack, who wonders why I might want
to do such a thing, consider the problem of trying to write a printf like
like formatting library in Ada. One interface (proposed in the programming
FAQ at adahome) goes something like this 

    type Format_Type is limited private;

    function  Format(S : in String) return Format_Type;
    procedure Printf    (Fmt : in Format_Type);
    function "&" (Fmt : in Format_Type; S : in String)    return
Format_Type;
    function "&" (Fmt : in Format_Type; I : in Integer)   return
Format_Type;

etc.

I was going to use the "Rosen trick" to modify data in the Fmt each time 
"&" is called. Unfortunately, that requires making the full view of
Format_Type limited and you run into the problem I described. 

Is there a cleaner way to implement that interface? 

-- Brian





  reply	other threads:[~1999-11-02  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-11-01  0:00 Return by reference Brian Rogoff
1999-11-02  0:00 ` Robert A Duff
1999-11-02  0:00   ` Brian Rogoff [this message]
1999-11-03  0:00     ` Lutz Donnerhacke
1999-11-03  0:00       ` Brian Rogoff
1999-11-04  0:00         ` Lutz Donnerhacke
1999-11-06  0:00           ` Brian Rogoff
1999-11-07  0:00             ` Lutz Donnerhacke
1999-11-04  0:00       ` Robert A Duff
1999-11-03  0:00     ` Matthew Heaney
1999-11-03  0:00       ` Matthew Heaney
1999-11-03  0:00       ` Brian Rogoff
1999-11-02  0:00 ` Lutz Donnerhacke
  -- strict thread matches above, loose matches on Subject: below --
2017-12-17 14:38 Jere
2017-12-17 14:57 ` Dmitry A. Kazakov
2017-12-18  0:51   ` Mehdi Saada
2017-12-18  8:18     ` Dmitry A. Kazakov
2017-12-18 10:47       ` Mehdi Saada
2017-12-18 12:16         ` Dmitry A. Kazakov
2017-12-18 22:41     ` Randy Brukardt
2017-12-18 22:33 ` Randy Brukardt
2017-12-19  1:29   ` Jere
replies disabled

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