我可以使用以下命令直接看到类型的原始子程序像这样的use all type
:
package Type_Package is
type T is null record;
procedure Action(X: T) is null;
end Type_Package;
with Type_Package;
procedure Use_Type is
use all type Type_Package.T;
X: Type_Package.T;
begin
Action(X);
end Use_Type;
但是,当我向内移动Type_Package
时,它似乎不起作用Use_Type
。
procedure Use_Type is
package Type_Package is
type T is null record;
procedure Action(X: T) is null;
end Type_Package;
use all type Type_Package.T;
X: Type_Package.T;
begin
Action(X);
end Use_Type;
我知道
gcc -c use_type.adb
use_type.adb:9:04: "Action" is not visible
use_type.adb:9:04: non-visible declaration at line 4
gnatmake: "use_type.adb" compilation error
当实例化通用包时,会发生相同的事情。对于例如,当我想使用Ada.Containers
中的数据类型时。
package Element_Set is
new Ada.Containers.Hashed_Sets(Element_Type, Hash, "=");
use all type Element_Set.Set;
这里的use type
子句似乎无效。
在子包或文件包中声明类型时如何使用类型通用包的实例化?
我不确定这是编译器错误还是预期的错误,但您的选择包括:
[整个包装使用“ use Type_Package”-可能是最简单但最繁重的操作
使用某些过程/函数重命名子句-这需要更多工作,但不会使整个程序包可见。例如:
procedure Hello is
package Type_Package is
type T is null record;
procedure Action(X: T) is null;
end Type_Package;
use all type Type_Package.T;
X: Type_Package.T;
procedure Action(X : Type_Package.T) renames Type_Package.Action;
begin
Action(X);
end Hello;
对于通用包,在库级别声明它们-这对嵌套包没有帮助,但对于通用包则有用。您可以对单个通用软件包执行以下操作:
Type_Package.ads
with Use_All_Type;
package Type_Package is new Use_All_Type;
Use_All_Type.ads
generic
package Use_All_Type is
type T is null record;
procedure Action(X: T) is null;
end Use_All_Type;
或对于多个通用软件包:
Type_Package.ads
with Use_All_Type;
package Type_Package is
package UAT is new Use_All_Type;
subtype T is UAT.T;
use all type UAT.T;
-- repeat for other generics
end Type_Package;
Use_All_Type.ads
generic
package Use_All_Type is
type T is null record;
procedure Action(X: T) is null;
end Use_All_Type;
这两种方法都可以与以下主要方法一起使用:
with Type_Package;
procedure Main is
use all type Type_Package.T;
X: Type_Package.T;
begin
Action(X);
end Main;
旁注:如果此处没有人明确回答这是否有意,您可以考虑向AdaCore发送错误报告。