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...
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.
Therefore :
private package Parent.Child is
type Child_Type is (A,B,C);
end Parent.Child;
...
package Parent is
procedure F;
end Parent;
...
with Ada.Text_Io;
with Parent.Child;
package body Parent is
procedure F is
begin
for A in Parent.Child.Child_Type'Range loop
Ada.Text_Io.Put_Line (Parent.Child.Child_Type'Image (A));
end loop;
end F;
end Parent;
Is 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 !
Therefore 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With