comp.lang.ada
 help / color / mirror / Atom feed
From: stt@houdini.camb.inmet.com (Tucker Taft)
Subject: Re: Generic formal package parameter question
Date: 1997/08/22
Date: 1997-08-22T00:00:00+00:00	[thread overview]
Message-ID: <EFBK29.GE4.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: Pine.SGI.3.95.970821223052.24A-100000@shellx.best.com


Brian Rogoff (bpr@shellx.best.com) wrote:

: 	There are two forms for generic formal package parameters

: with package P is new G(<>);

: or

: with package P is new G( Q1, Q2, ..., QN );

: Why there isn't an intermediate form where some, but not necessarily 
: all, of the parameters appear in the parameter list, allowing us to 
: better represent the relationships between the parameters? 

: For example, suppose we have two generic signature packages, Assignable 
: and Composable

: generic
:     type Assignable_Type is limited private;
:     with procedure Assign( From : in out Assignable_Type;
:                            To : out Assignable_Type );
: package Assignable is end;

: generic
:     type Comparable_Type is limited private;
:     with function Compare ( X, Y : Comparable_Type ) 
:         return Comparison_Type; -- Boolean or three-valued
: package Comparable is end;

: and we want a third package to have a generic formal parameter which is 
: both Assignable and Comparable. I'd like to be able to express it like 
: this

: generic 
:     type Value_Type is private;
:     with package Comparable_Values is 
:         new Comparable( Value_Type, <> );  -- Illegal
:     with package Assignable_Values is 
:         new Assignable( Value_Type, <> );  -- Illegal
: package Assignable_Comparable is end;

: but Ada forces an all or nothing approach here, and I have to choose
: nothing (<>) rather than be able to express those constraints that I can,
: in this case  that both of the signature packages are instantiated with
: Value_Type. This seems really against the spirit of Ada to me, so I'm
: curious as to the reasons for this limitation. 

During the 9X design process, whenever we posed a question like this, 
we eventually learned to put ourselves in the following mind set:
What is the simplest set of rules that will give us the essential 
power and flexibility desired (and not get tossed out completely 
by the distinguished reviewers ;-)?

One can always make features more complicated in an attempt to match
perfectly all problems, but during the design process we had to focus on
providing language features as building blocks, which programmers could 
combine in various ways to solve their various problems.  

In particular, I believe there is a legal combination that is roughly 
equivalent to your solution.  First, it is useful to recognize that any use 
of (<>) is functionally equivalent to having a bunch of additional formal
parameters, one per parameter of the template package, and then
use those parameters in the formal package as actuals.  Furthermore,
all formal subprogram parameters can be defaulted, presuming there
is an appropriate "use" clause in effect.

For example:

    with package Assignable_Values is new Assignable(<>);

is functionally equivalent to:

    type Assignable_Type is private;
    with procedure Assign( From : in out Assignable_Type;
                           To : out Assignable_Type ) is <>;
    with package Assignable_Values is new Assignable(
      Assignable_Type, Assign);

Clearly (<>) is more convenient for the instantiator, but it
does not actually add any additional semantic capability.

Given that (<>) can be replaced by explicit (possibly defaulted) 
parameters, any sort of partial (<>, param, <>) could also be replaced by 
explicit parameters.  On the other hand, there are things which
the version with explicit parameters can do that the (<>) form
cannot, such as indicate that two instances must have the same
actual parameter for corresponding formal parameters, as above.

In any case, switching over to (<>) for both of your formal
packages is not equivalent to the above.  However, the following
is equivalent, albeit not quite as convenient (though the use of
a "use" clause at the instantiation point seems to provide
adequate convenience):

   generic 
       with package Comparable_Values is 
           new Comparable( <> );
       use Comparable_Values;
       with procedure Assign( From : in out Comparable_Type;
                              To : out Comparable_Type ) is <>;
       with package Assignable_Values is 
           new Assignable( Comparable_Type, Assign);
   package Assignable_Comparable is end;

Now an instantiation of this could be:
   with C_Values;
   with A_Values; use A_Values;  -- "use" makes operation(s) visible
   package A_C_Values is new Assignable_Comparable(
     Comparable_Values => C_Values,
     Assignable_Values => A_Values);
     -- "Assign" defaults properly

In this particular case, you could actually drop the 
"Assignable_Values" formal completely, since it is
not adding anything over and above the "with procedure Assign".
The instantiator would then just "use" the appropropriate
instance of the generic signature Assignable, rather than
passing it in explicitly as a parameter.  However, that may
appear a bit odd at the instantiation point.  I.e:

    with C_Values;
    with A_Values; use A_Values;
    package A_C_Values is new Assignable_Comparable(C_Values);

since the relevant part of A_Values is all coming in via defaulted
formal subprogram parameters.

: -- Brian

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




  reply	other threads:[~1997-08-22  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-08-21  0:00 Generic formal package parameter question Brian Rogoff
1997-08-22  0:00 ` Tucker Taft [this message]
1997-08-22  0:00   ` Brian Rogoff
replies disabled

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