我需要制作一个通用包,它的形参应该是访问子程序的类型。这种类型的对象被传递给这个包的子例程,我必须检查它是否为空相等。
generic
type Func_Type is private;
package Gen_Package is
function Check(Func : Func_Type) return Boolean is
(Func = null);
end;
但这不能编译,因为比较的左操作数没有访问类型。
最佳答案
您可以利用访问类型对象的初始值为 null
的事实。 :
generic
type Func_Type is private;
-- make comparison operator available. `is <>` means it does not
-- need to be given explicitly if it is visible at the site of instantiation.
with function "=" (Left, Right : Func_Type) return Boolean is <>;
package Gen_Package is
function Check (Func : Func_Type) return Boolean;
end Gen_Package;
package body Gen_Package is
function Check (Func : Func_Type) return Boolean is
-- not constant because we cannot give an initialization expression.
-- if Func_Type is an access type, Null_Value will be initialized with null.
Null_Value : Func_Type;
begin
return Func = Null_Value;
end Check;
end Gen_Package;
关于generics - 如何声明对泛型中任何子程序形式类型的访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67606221/