Access child package declarations

Can I access child package declarations from the 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's impossible to use child packages ...

+3
source share
3 answers

, ; , Child , F Parent. , . Q & A : , .

+1

, private, , , .

:

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;

, , (, F), , , !

, , , , .

+1

Julio, types declared in spec file (mytypes.ads)

package Mytypes is

   type Fruit is (Apple, Pear, Pineapple, Banana, Poison_Apple);
   subtype Safe_Fruit is Fruit range Apple .. Banana;

end Mytypes;

... restrained him in several others:

with Mytypes;
package Parent is

   function Permission (F : in Mytypes.Fruit) return Boolean;

end Parent;

...

package body Parent is

   function Permission (F : in Mytypes.Fruit) return Boolean is
   begin
      return F in Mytypes.Safe_Fruit;
   end Permission;

end Parent;

...

package Parent.Child is

   procedure Eat (F : in Mytypes.Fruit);

end Parent.Child;

...

with Ada.Text_Io;
package body Parent.Child is

   procedure Eat (F : in Mytypes.Fruit) is
   begin
      if Parent.Permission (F) then
         Ada.Text_Io.Put_Line ("Eating " & Mytypes.Fruit'Image (F));
      else
         Ada.Text_Io.Put_Line ("Forbidden to eat " & Mytypes.Fruit'Image (F));
      end if;
   end Eat;

end Parent.Child;

...

with Mytypes;
with Parent.Child;

procedure Main is

begin

   for I in Mytypes.Fruit'Range loop
      Parent.Child.Eat (I);
   end loop;

end Main;

It compiles:

$ gnatmake main.adb
gcc-4.4 -c parent-child.adb
gnatbind -x main.ali
gnatlink main.ali

Performed:

$ ./main
Eating APPLE
Eating PEAR
Eating PINEAPPLE
Eating BANANA
Forbidden to eat POISON_APPLE

Is that what you tried?

+1
source

All Articles