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: Robert A Duff Subject: Re: Return by reference Date: 1999/11/02 Message-ID: #1/1 X-Deja-AN: 543578343 Sender: bobduff@world.std.com (Robert A Duff) References: Organization: The World Public Access UNIX, Brookline, MA Newsgroups: comp.lang.ada Date: 1999-11-02T00:00:00+00:00 List-Id: 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. 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. On the other hand, if you want true run-time checks, you can use access parameters. Some extra dope is normally passed with an access parameter. - Bob