From: loftus@wpllabs.UUCP (William Loftus)
Subject: Re: factoring with generics
Date: 14 Mar 91 03:45:15 GMT [thread overview]
Message-ID: <loftus.5211@wpllabs.UUCP> (raw)
In-Reply-To: 45441@ut-emx.uucp
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 <f1> & <f2> differ only in one subprogram call ]
>
>>It occurs to me that it might be wise to implement these
>>as instances of one generic function (<g_f>, say) which takes the
>>crucial differing subprogram as a generic parameter.
>>To do this, I could:
>
>> 1) put the GENERIC DECLARATION and BODY of <g_f> in the body of <p>.
>> In this case, the implementations of <f1> & <f2> would
>> instantiate <g_f>.
>
>> 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 <f1> 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
next prev parent reply other threads:[~1991-03-14 3:45 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1991-03-08 23:36 factoring with generics David A. Hasan
1991-03-11 21:20 ` David A. Hasan
1991-03-14 3:45 ` William Loftus [this message]
1991-03-15 17:39 ` David A. Hasan
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox