comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Access to generic formal parameters in an generic package instantiation
Date: Mon, 30 Jul 2012 11:33:21 -0700 (PDT)
Date: 2012-07-30T11:33:21-07:00	[thread overview]
Message-ID: <383e776c-f377-4ede-8560-cbb54fde3c87@googlegroups.com> (raw)
In-Reply-To: <87a9yi5t7j.fsf@mid.deneb.enyo.de>

On Sunday, July 29, 2012 11:16:00 AM UTC-7, Florian Weimer wrote:
> If G is a generic package and P is one of its formal parameters, it is
> legal to refer to G.P where G is visible? 

No, unless you're inside G.  Outside of G, you can't refer to any entities inside G.

I wonder if you're referring to a generic *instance*, though.  It's helpful to keep the terminology straight:

   generic
       type T is private;
   package Gen_Package is ...

   package Instance is new Gen_Package (Integer);  -- for example

Gen_Package is a "generic package".  Outside of Gen_Package, you can't refer to Gen_Package.T and it wouldn't make any sense.  Instance is not a "generic package"; it's a "generic instance", or an "instance of a generic package".

However, you still can't refer to Instance.T.  I think the theory was probably that the code knows what Instance.T is because it's right there in the parameter list when Gen_Package is instantiated, so it isn't necessary to allow Instance.T.  

In this case
    generic
       with INST is new Gen_Package (<>);
    package Gen_Package_2 is ...

INST is a "generic formal package", not a "generic package".  It represents an instance of Gen_Package, but of course we don't know what the instance is until we instantiate Gen_Package_2.  Within Gen_Package_2, you can refer to INST.T, because (unlike the previous example) the code in Gen_Package_2 doesn't know what INST.T is, because there's only a <> in the parameter list of the generic formal package declaration.

Hope this helps a little,


> Does this depend on the
> kind of entity, or whether the formal part uses <>?
> 
> 
> 
> Has anybody tried to use generic formal packages to emulate Standard
> 
> ML signatures?  Is it possible to express SML "where" constraints, 
> that is, specify that two types in two formal packages are the same,
> without break down the formal packages to their components?

You can do something like this:

  generic
     with package INST is new Gen_Package (<>);
     with package ANOTHER_INST is new Another_Gen_Package (T => Inst.T);
  package Gen_Package_3 is ...

Now when you instantiation 

  package Some_Inst is new Gen_Package_3 (P1, P2);

the instantiation is legal only if P1 and P2 were instantiated with the same type for T.  This seems like what you're looking for.  (P.S. This sort of thing is also legal starting with Ada 2005:


  generic
     with package INST is new Gen_Package (<>);
     with package ANOTHER_INST is new Another_Gen_Package (T => Inst.T,
                                                           others => <>);
  package Gen_Package_3 is ...
 
That puts a constraint on the type T used to instantiate Another_Gen_Package, while not putting any constraints on any other actual parameters.)

Hope this helps a little,

                          -- Adam




  parent reply	other threads:[~2012-08-06 15:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-29 18:16 Access to generic formal parameters in an generic package instantiation Florian Weimer
2012-07-29 19:36 ` Dmitry A. Kazakov
2012-07-29 21:31   ` Florian Weimer
2012-07-30 18:37     ` Adam Beneschan
2012-07-30 18:33 ` Adam Beneschan [this message]
2012-07-31  7:48 ` Georg Bauhaus
2012-07-31 15:29   ` Adam Beneschan
2012-07-31 16:50     ` Georg Bauhaus
replies disabled

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