comp.lang.ada
 help / color / mirror / Atom feed
From: bobduff@world.std.com (Robert A Duff)
Subject: Re: Instantiation of child generics?
Date: 1996/07/26
Date: 1996-07-26T00:00:00+00:00	[thread overview]
Message-ID: <Dv5LJ1.L8q@world.std.com> (raw)
In-Reply-To: 4t9itp$imd@goanna.cs.rmit.edu.au


In article <4t9itp$imd@goanna.cs.rmit.edu.au>,
Dale Stanbrough  <dale@goanna.cs.rmit.edu.au> wrote:
>Hi,
>
>I'ld like to create an abstract generic queue package with bounded
>and unbounded children. However I have been thwarted in all attempts
>to instantiate them. Also I don't really understand the rules for
>instantiaion of child generics. Can anyone explain them?
>
>
>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);

You need to instantiate the parent first:

    with queues;
    package integer_queues is new queus(element => integer);

Now, conceptually, for every child of queues, there is a generic child
of integer_queues.  But these are not visible unless you "with" the
corresponding child of queues.  Also, they are somewhat mythical -- the
compiler will normally not create them unless needed (now can it? -- it
can't know about all those children at compile time).

    with integer_queues;
    with queues.bounded; -- Makes Integer_Queues.Bounded visible.
    package bounded_integer_queues is new integer_queues.bounded(size => 5);

Or, you could make the instance a child:

    with integer_queues;
    with queues.bounded;
    package integer_queues.bounded_integer_queues is new
        integer_queues.bounded(size => 5);
    -- You can't call this instance integer_queues.bounded, because
    -- that's the name of a generic.

Or, you could nest them inside another package:

    with queues; -- Not strictly necessary, but I like to write this.
    with queues.bounded;
    package My_Queues is
        package int_queues is new queus(element => integer);
        -- Here, int_queues.bounded is visible, because queues.bounded
        -- is "with"ed, so we can instantiate it:
        package bounded is new int_queues.bounded(size => 5);
        ...
    end My_Queues;

>results in Gnat telling me "invalid prefix in selected component "queues""
>
>Also will my (re)use of the identifier "queue" in the packages cause any
>problems for me?

Should be OK, but I don't particularly like that style.

- Bob




      parent 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
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 [this message]
replies disabled

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