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,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,965fbd9798ca1677 X-Google-Attributes: gid103376,public From: Brian Rogoff Subject: Re: Return by reference Date: 1999/11/02 Message-ID: #1/1 X-Deja-AN: 543789765 References: Content-Type: TEXT/PLAIN; charset=US-ASCII X-Trace: nntp1.ba.best.com 941598953 218 bpr@206.184.139.136 MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 1999-11-02T00:00:00+00:00 List-Id: On Tue, 2 Nov 1999, Robert A Duff wrote: > Brian Rogoff 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