Is it possible to access child package declarations from a parent package ?
-- parent.ads
package Parent is
procedure F(A : Child_Type);
end Parent;
-- parent-child.ads
package Parent.Child is
type Child_Type is (A, B, C);
end Parent.Child;
The nested version works fine :
-- parent.ads
package Parent is
package Child is
type Child_Type is (A, B, C);
end Child;
use Child;
procedure F(A : Child_Type);
end Parent;
And maybe there is another way to do this since I think it is not possible using child packages...
trashgod :
In general, no; the second example works because the specification of Child is known when F is declared in Parent. In light of your previous question on this topic, it may be that you want a clean way to separate multiple implementations of a common specification. This related Q&A discusses two approaches: one using inheritance and the other using a library-based mechanism at compile-time.",
2012-05-07T11:09:34
NWS :
I think what you are looking for is a private child package, this generally behaves in the same way as your nested example, but you cannot access it outside of its parent body.\n\nTherefore : \n\nprivate package Parent.Child is\n type Child_Type is (A,B,C);\nend Parent.Child;\n\n\n...\n\npackage Parent is\n procedure F;\nend Parent;\n\n\n...\n\nwith Ada.Text_Io;\nwith Parent.Child;\npackage body Parent is\n procedure F is\n begin\n for A in Parent.Child.Child_Type'Range loop\n Ada.Text_Io.Put_Line (Parent.Child.Child_Type'Image (A));\n end loop;\n end F;\nend Parent;\n\n\nIs ok to compile, but remember if you with the child in the parent spec (like you do with the parameter to F), you will get a circular dependency as children require their parents to exist first ! \n\nTherefore it really depends on what you want to be public to both the parent and the child whether this is an actual solution to your problem.",
2012-05-08T07:38:56