我有一个带有以下签名的通用包
generic
type T is private;
with function "="(Left : T; Right : T) return Boolean is <>;
with function Wide_Wide_Image(Self : T) return Wide_Wide_String is <>;
package Generics.Testing.Assertions is
Run Code Online (Sandbox Code Playgroud)
它有一个带有以下签名的子包
generic
with function "<"(Left : T; Right : T) return Boolean is <>;
with function ">"(Left : T; Right : T) return Boolean is <>;
package Generics.Testing.Assertions.Comparisons is
Run Code Online (Sandbox Code Playgroud)
我正在尝试在另一个包中实例化它们,但有一个有趣的问题。
这工作正常:
package Integer_Assertions is new Generics.Testing.Assertions(
Integer,
Wide_Wide_Image => Integer'Wide_Wide_Image);
Run Code Online (Sandbox Code Playgroud)
奇怪的是当我尝试使用以下方法实例化子包时:
package Integer_Comparisons is new Integer_Assertions.Comparisons;
Run Code Online (Sandbox Code Playgroud)
GPS正常地找到了Comparisons包裹,正如它应该的那样。Integer_Assertions但编译器出现以下两个错误:
missing "with Integer_Assertions.Comparisons;"
Run Code Online (Sandbox Code Playgroud)
和
"Comparisons" not declared in "Integer_Assertions"
Run Code Online (Sandbox Code Playgroud)
好的?但 IntelliSense 发现它很好。不过,我已经有一段时间没有做过太多 Ada 开发了,所以也许我忘记了如何实例化泛型的泛型子代。
所以我尝试使用完全限定的非实例名称:
missing "with Integer_Assertions.Comparisons;"
Run Code Online (Sandbox Code Playgroud)
失败的原因是:
invalid prefix in selected component "Generics.Testing.Assertions"
Run Code Online (Sandbox Code Playgroud)
我记得应该是这样。
那么我如何实际实例化包内的子项呢?
哦,发生的事情是依赖关系图不准确,原因是您需要使用层次结构中最深的泛型。(例如with Generics.Testing.Assertions.Comparison;)
然后你这样做:
package Integer_Assertions is new Generics.Testing.Assertions
( Integer, Wide_Wide_Image => Integer'Wide_Wide_Image );
package Integer_Comparisons is new Integer_Assertions.Comparisons;
Run Code Online (Sandbox Code Playgroud)
这样做的原因是,如果没有with完整的依赖路径,就无法找到真正的依赖关系;这是嵌套和分层包组织之间的区别:您不需要(也不能)with嵌套单元,您必须使用您使用的最深的分层单元。
| 归档时间: |
|
| 查看次数: |
196 次 |
| 最近记录: |