Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ada:重新导出枚举类型值_Ada - Fatal编程技术网

Ada:重新导出枚举类型值

Ada:重新导出枚举类型值,ada,Ada,我有一个ada包,格式为 package C is type Test is (Test_1, Test_2, Test_3); end C; with C; package B is subtype Test is C.Test; end B; with B; package A is subtype Test is B.Test; Item: Test := Test_1; end A; 和另一个以 package C is type Test

我有一个ada包,格式为

package C is
   type Test is (Test_1, Test_2, Test_3);   
end C;
with C;
package B is
   subtype Test is C.Test;
end B;
with B;
package A is
   subtype Test is B.Test;   
   Item: Test := Test_1;
end A;
和另一个以

package C is
   type Test is (Test_1, Test_2, Test_3);   
end C;
with C;
package B is
   subtype Test is C.Test;
end B;
with B;
package A is
   subtype Test is B.Test;   
   Item: Test := Test_1;
end A;
还有一种形式是

package C is
   type Test is (Test_1, Test_2, Test_3);   
end C;
with C;
package B is
   subtype Test is C.Test;
end B;
with B;
package A is
   subtype Test is B.Test;   
   Item: Test := Test_1;
end A;
天真地说,我希望在B中定义的子类型(后来被a定义为子类型)能够访问原始类型的成员。但是,经检查,C的成员在B的范围内甚至不可见。这可以通过添加
使用C来解决
,这在某种程度上似乎是一个查找解决方案,但是要在a中使用它,您必须用c添加
;使用c到每个可传递地依赖于C的包。这可能会导致混淆,因为您不应该隐式地使用C


我希望能够“重新导入”这些类型,以便更好地抽象程序的层。

如果您将包A更改为

with B;
package A is
   subtype Test is B.Test;
   use all type Test;
   Item: Test := Test_1;
end A;
代码可以编译。不确定这是否有帮助

这是ADA2012的一个特性;见和(8.1)<代码>使用所有类型
使该类型的基本操作可见(包括枚举文字)

…一切都很好

Ada有可见性的概念,有很多规则定义事物何时、何地以及如何可见。理解可见性是理解Ada的关键。关于可见性的最佳讨论之一是在中。您遇到的是可见性规则的结果。这比枚举文字更进一步:

package Pack is
   type Int is range -(2 ** 15) + 1 .. 2 ** 15 - 1;
end Pack;

with Pack;
procedure Proc is
   subtype Int is Pack.Int;
   -- None of the operators of Int ("+", "*", ...) are directly visible here
begin
   null;
end Proc;

包规范中的声明只有在包外使用虚线表示法才可见,除非您显式地使它们在其他地方直接可见<代码>使用子句是使其可见的一种方法;另一些是重命名,对于枚举文字,声明使用点符号初始化的类型的常量。

如果确实要重新导出值,可以执行以下操作:

与C;
B包是
亚型检验为C检验;
功能测试_1返回测试为(C.Test_1);
功能测试_2返回测试为(C.Test_2);
功能测试_3返回测试为(C.Test_3);
B端;
不幸的是,您不能使用命名数字,因为枚举不是数字类型。您可以将这些函数改为普通常量,但从概念上讲,这将在精化时执行代码(编译器可能会对其进行优化,但您不能再使用
pragma preeloarate

这允许您使用
B.Test_1
等访问
A
中的文字。这也是一种适当的抽象,因为
A
将不再依赖于
C
中定义的文字(您可以在不影响
A
的情况下重命名
C
中的文字,但需要更新
B
以将新名称映射到原始名称)


简单地将文字名称导入
A
的名称空间并不是一种抽象。

查看“使用类型”(可能是“使用类型c.test”)为了在不暴露所有C的情况下使类型可见@BrianDrummond我主要担心的是,C不是一个明显的可传递依赖项,而且,这需要我修改我试图应用它的许多代码区域,而如果有类似typedef的功能,重构将在很大程度上是透明的ose,但仍然不允许在其他语言中使用类似于typedef的功能。不幸的是,我开始怀疑在ada中不可能使用此功能。这确实非常接近于实现我的目标。请添加一个解释,说明这如何回答问题如果您想要不同的类型,一切都很好。一切都不是w最后,它取决于OP想要什么:子类型,或者包a的用户可以忽略包C的新类型。