Julio,在规范文件 (mytypes.ads) 中声明的类型
package Mytypes is
type Fruit is (Apple, Pear, Pineapple, Banana, Poison_Apple);
subtype Safe_Fruit is Fruit range Apple .. Banana;
end Mytypes;
...在其他几个中使用它:
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;
它编译:
$ gnatmake main.adb
gcc-4.4 -c parent-child.adb
gnatbind -x main.ali
gnatlink main.ali
它运行:
$ ./main
Eating APPLE
Eating PEAR
Eating PINEAPPLE
Eating BANANA
Forbidden to eat POISON_APPLE
这是你试过的吗?