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=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!emory!gatech!udel!burdvax!wpllabs!loftus From: loftus@wpllabs.UUCP (William Loftus) Newsgroups: comp.lang.ada Subject: Re: factoring with generics Message-ID: Date: 14 Mar 91 03:45:15 GMT References: <45358@ut-emx.uucp> <45441@ut-emx.uucp> Organization: WPL Laboratories List-Id: In article <45441@ut-emx.uucp> hasan@ut-emx.uucp (David A. Hasan) writes: >In article <45358@ut-emx.uucp> I wrote: > >>PACKAGE p IS >> TYPE someType IS ...; >> FUNCTION f1(arg1,arg2 : IN someType) RETURN someType; >> FUNCTION f2(arg1,arg2 : IN someType) RETURN someType; >>END p; > >[ where & differ only in one subprogram call ] > >>It occurs to me that it might be wise to implement these >>as instances of one generic function (, say) which takes the >>crucial differing subprogram as a generic parameter. >>To do this, I could: > >> 1) put the GENERIC DECLARATION and BODY of in the body of

. >> In this case, the implementations of & would >> instantiate . > >> But of course, this is hogwash: generic instantiations >> are *declarations* and cannot be used to define the >> implementation. So, as an alternative... > >Or *is* it hogwash? This technique is used by Habermann & Perry >in their book "Ada for Experienced Programmers". In fact, the >LRM seems to indicate that a generic instantiation can be used >as the implementation of a subprogram. (I reached this >conclusion by threading my way through the BNF -- a technique >I often use when I can't find an explicit discussion of my >problems.) > >The DEC compiler objects to the presence of generic >instantiations such as > > FUNCTION f1 is NEW g_f( ... ); > >on the basis that it constitutes a redclaration of as it >appears in the package spec. > >Anyone know whether this is legal or not? It is not legal. A generic instantiation introduces both a spec and body (see 12.2(1) and 12.2(2)). A generic instantiation of a procedure in a package body introduces a spec and body for that routine into the package body. If there is a specification of a homograph in the package specification then there is a redeclaring of the specification of the subprogram in the package body, and therefore is illegal. However, this has been submitted by ALWG as a consideration for Ada 9x. Also notice that you cannot use a renames solely as an implemenation, either. -- William Loftus (215) 668 3661 WPL Laboratories, Inc. UUCP: loftus@wpllabs.UUCP P.O. Box 111 ARPA: loftus!wpllabs@prc.unisys.com 216 Wynne Lane Penn Valley, PA 19072 Ada and Unix Software Consultants