Visibility between generic child packages

I have a generic package A. Then there are two generic child packages named A.c1 and A.c2. The children can see the stuff in the parent package A, which is ok so far. But how can I make the siblings c1 and c2 see each other ?

Siblings can’t normally see each other. They can look “up” but not “sideways”. If the package wasn’t generic, you could make a sibling private package and then the siblings can see that (it acts like a parent private section).

I don’t know if that works for generics though. That would seem odd since you have to instantiate them.

You could try ti though. See if you can make a non generic private sibling package and then you can put shared code in there if it works. I don’t think you can make a non generic child package but private packages are new enough I don’t know if they change any rules or not.

Thanks for your reply. Yes, the sibling can look up and can call a procedure in the parent package.
My problem is that sibling c1 needs to call a procedure p in c2, which is “sideways looking”. I can add a use clause like “use c2” in c1. But calling procedure p from inside c1 requires an instantiation of child c2.
So the only solution seems to move procedure p into the parent package.

This doesn’t appear to be true for generics, but non-generic siblings can see each other (whether or not they’re private). You still need to with them.

1 Like

children of generic packages are also generic. You have to create instances of them before you can call anything

You could also add a “with package X is new Parent.C2(<>);” formal to your c1 generic parameter list.

1 Like

The problem is that child packages are conceptually nested in their parents. When the parent is a generic, the child package doesn’t really exist until the parent has been instantiated. The child is then a child of the instance, not of the generic, which is why it’s instantiated using the name of the instance, not the name of the parent. So how can the child of a generic name its siblings when they don’t exist until their parent is instantiated?

1 Like

One generic child package may “with” another. The model to keep in your mind is that the net effect of a “with” clause is to (conceptually) insert a declaration of the named unit into its parent unit. When a generic child "with"s another, it is as though the other generic child is now a nested generic within its parent generic. So, for example, the following works:

generic
    type T is private;
package Gen is
    X : T;
end Gen;

generic
    type T1 is private;
package Gen.C1 is
    Y : T1;
    Z : Gen.T;
end Gen.C1;

with Gen.C1;
generic
    type T2 is private;
package Gen.C2 is
    A : T2;
    B : Gen.T;
    package IC1 is new Gen.C1 (Integer);
    C : Integer := IC1.Y;
end Gen.C2;

In the above, the generic child Gen.C2, after "with"ing its sibling Gen.C1, can now instantiate Gen.C1, because C1 is conceptually a nested generic within Gen.

You will at some point have to instantiate Gen itself to produce some instance Inst, and then instantiate Inst.C2 to produce an instance of the child Gen.C2, and inside there you will be able to reference the instance IC1 of Gen.C1 which C2 has defined.

3 Likes

It took some weeks to get back to this topic. So what sttaft worte is the solution.
Great ! That works. Thank you.

They generally can’t, though that’s not strictly true. For child-units, you can use context-clauses in the bodies to depend on them, in all cases, and in special cases the specification… with the private with, limited with, and limited private with.

The thing to really remember with child-generics is that given G, G.A, and G.B, and and instantiation of G, I, you need to instantiate I.A and I.B.