Home:ALL Converter>Access child package declarations

Access child package declarations

Ask Time:2012-05-07T17:48:45         Author:Julio Guerra

Json Formatter

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...

Author:Julio Guerra,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/10479955/access-child-package-declarations
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
yy