comp.lang.ada
 help / color / mirror / Atom feed
From: stt@henning.camb.inmet.com (Tucker Taft)
Subject: Re: Instantiation of child generics?
Date: 1996/07/26
Date: 1996-07-26T00:00:00+00:00	[thread overview]
Message-ID: <Dv5LDM.IsI.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: 4t9itp$imd@goanna.cs.rmit.edu.au


Dale Stanbrough (dale@goanna.cs.rmit.edu.au) wrote:
: ... Also I don't really understand the rules for
: instantiaion of child generics. Can anyone explain them?

You should think of a child of a generic as a nested generic,
which is only visible to those who choose to mention it in a with clause.
Since it is conceptually a nested generic, you can only instantiate
it once you have already instantiated the enclosing generic.
Given your example below, you would first instantiate "queues":

   with queues;
   package queue_inst is new queues(Integer);

Then you would instantiate the "nested" generic "bounded" as follows:

   with queue_inst;  -- The instance of the enclosing generic
   with queues.bounded;  -- Makes the nested generic visible in all instances
   package bounded_inst is new queue_inst.bounded(200);

If "bounded" had been truly nested, rather than a child, the only difference
would be that you would omit the "with queues.bounded;"
The effect of "with queues.bounded;" is to "plug" the child "bounded"
into its parent generic "queues", meaning that "bounded" becomes visible
in all instances of "queues."

We struggled during the 9X process with children of generics, since
it is certainly a bit counterintuitive that you "with" "queues.bounded"
whereas you instantiate "queue_inst.bounded."  However, if you think
of a "with" clause as having the effect of "plugging" a child into
its parent unit, then you should be able to understand why children of
generics work the way they do.


: e.g. 

: 	generic
: 		type element is private;
: 	package queues is 
: 		type queue is abstract tagged null record;
: 		
: 		procedure init return queue is abstract;
: 		
: 		procedure enqueue(item:in out queue; item:element) is abstract;

: 		-- etc
: 	end queues;
: 	
: 	
: 	generic
: 		size	:natural;
: 	package queues.bounded is
: 			
: 		type queue is new queues.queue with private;
: 		
: 		procedure init return queue is abstract;
: 		
: 		procedure enqueue(item:in out queue; item:element) is abstract;

: 	private
: 		type queue is new queues.queue with null record; -- for now...
: 	end;


: How do I instantiate this?

: 	with queues.bounded;
: 	package myQ is new queues.bounded(integer, 5);
: 	
: results in Gnat telling me "invalid prefix in selected component "queues""

GNAT is correct; the prefix to "bounded" should be an instantiation
of queues, not queues itself.

: Also will my (re)use of the identifier "queue" in the packages cause any
: problems for me? It seems to compile ok, but i'm concerned that there might
: be some nasty visibility issue lurking around the corner...

Only if you "with" and "use" both packages.  Even then, you can still
use a full expanded name where there is a name conflict.

: Thanks,

: Dale

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




  reply	other threads:[~1996-07-26  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-07-26  0:00 Instantiation of child generics? Dale Stanbrough
1996-07-26  0:00 ` Tucker Taft [this message]
1996-07-26  0:00   ` Kevin J. Weise
1996-07-27  0:00     ` Robert A Duff
1996-07-30  0:00       ` Dale Stanbrough
1996-07-30  0:00         ` Robert A Duff
1996-07-26  0:00 ` Robert A Duff
replies disabled

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