Dima
Dima

Reputation: 1311

Creating generic child packages in Ada 95

I have package p1 and two child packages: p1.child1 and p1.child2 All packages are generic. I'm trying to create an instance of p1.child1 in p1.child2:

package body p1inst.child2 is

   package p1inst is new p1;
   use p1inst;

   package p1_child1inst is new p1inst.child1;
   use p1inst;

I got an error: Instansiation of "p1" within itself. How can I create an instance of generic package p1.child2 into p1.child1?

Upvotes: 3

Views: 1145

Answers (1)

trashgod
trashgod

Reputation: 205785

As discussed in Ada Programming: Advanced generics, it may help to distinguish between a generic unit and an instance of that generic unit. The compiler is telling you that you can't create an instance of p1 in a child of p1, because "Children of a generic unit must be generic, no matter what."

Instead, create an instance of p1 elsewhere, and use that instance to create an instance of each of the children of p1.

Addendum: As another concrete example, procedure Jumble creates an instance of Ada.Strings.Bounded:

Max_Word  : constant Positive := 24;
package ASB is new Ada.Strings.Bounded.Generic_Bounded_Length(Max_Word);

Later, the procedure uses that instance to create an instance of the generic child, Ada.Strings.Bounded.Hash:

function Hash is new Ada.Strings.Bounded.Hash(ASB);

Upvotes: 4

Related Questions