泛型参数的声明不够详尽,无法给出类型关系(子类型),并且这些信息只是丢失了......例如:
-- generic_p.ads
generic
type Index_Range_Type is range <>;
type Count_Range_Type is range <>;
procedure Generic_P (I : Index_Range_Type, C : Count_Range_Type);
-- generic_p.adb
procedure Generic_P (I : Index_Range_Type, C : Count_Range_Type) is
begin
if I = C then -- oops : cannot compare different types...
-- ...
end if;
end Generic_P;
-- main.adb
procedure Main is
type Index_Range_Type is 0 .. 512;
subtype Count_Range_Type is Index_Range_Type range 1 .. Index_Range_Type'Last;
procedure P is new Generic_P (Index_Range_Type, Count_Range_Type);
I : Index_Range_Type := 33;
C : Count_Range_Type := 42;
begin
if I = C then -- Ok : Count_Range is a subset of Index_Range, they can be compared
-- ...
end if;
P (I, C);
end Main;
这为 generic_p.adb 中的比较提供了以下错误:invalid operand types [...] left operand has type "Index_Range_Type" [...] right operand has "type Count_Range_Type"
。子类型在通用过程中不可见。
有没有办法指定泛型参数之间的关系?
更多信息
我真的需要Count_Range_Type
作为程序的一个参数才能添加另一个需要的参数Count_Range_Type
。
-- generic_p.ads
generic
type Index_Range_Type is range <>;
type Count_Range_Type is range <>;
with procedure F (C : Count_Range_Type);
procedure Generic_P (I : Index_Range_Type, C : Count_Range_Type);
我不能直接使用类型,我需要 P 绝对通用和独立。