generics - 如何声明对泛型中任何子程序形式类型的访问?

标签 generics ada

我需要制作一个通用包,它的形参应该是访问子程序的类型。这种类型的对象被传递给这个包的子例程,我必须检查它是否为空相等。

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/

相关文章:

Java泛型抽象类返回类型不匹配

graph - Ada:绘制二维图

java - 无法使用不同类型的参数实现接口(interface)

oop - Oberon 的面向对象模型与标准 OOP 有何不同?

containers - 我们什么时候应该使用 Indefinite_Hashed_Maps 或 Hashed_Maps

artificial-intelligence - Ada 中的 AI 库框架

string - 将无界字符串转换为整数 Ada

c# - 为什么简单的 List<T> 似乎比 ArrayList 慢?

Java 泛型类型和方法

c# - 您如何将通用列表投影到字典中?