我需要制作一个通用包,它的形参应该是访问子程序的类型。这种类型的对象被传递给这个包的子例程,我必须检查它是否为空相等。
generic
type Func_Type is private;
package Gen_Package is
function Check(Func : Func_Type) return Boolean is
(Func = null);
end;
Run Code Online (Sandbox Code Playgroud)
但这不能编译,因为比较的左操作数没有访问类型。
您可以利用访问类型对象的初始值是这样的事实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;
Run Code Online (Sandbox Code Playgroud)