generics - 实例化具有对变体记录的子类型访问权限的包时出现问题

标签 generics ada

我尝试使用 Ada 2012 编译器编译 Ada 95 程序。然而,通用包 Garbage_Collector 的实例化存在问题。 .亚型 A_Term包实例化中不接受:

prolog.adb:27:15: designated type of actual does not match that of formal "Link"
prolog.adb:27:15: instantiation abandoned
我试图改变A_Termtype A_Term is access A_Node; .然后包将实例化,但其余代码中断。自 Ada 95 以来发生了一些变化,我如何使其在 Ada 2012 中工作?
procedure Prolog is

   generic
      type Item is limited private;
      type Link is access Item;
   package Garbage_Collector is
      procedure Get (L : in out Link) is null;
   end Garbage_Collector;

   type Node_Tag is (A, B);

   type Node (Tag : Node_Tag);
   type Term is access Node;

   type Node (Tag : Node_Tag) is
      record
         case Tag is
            when A => null;
            when B => null;
         end case;
      end record;

   subtype A_Node is Node (A);
   subtype A_Term is Term (A);
   package Gc_A is new Garbage_Collector
     (Item => A_Node,
      Link => A_Term);

   T : Term;
begin
   Gc_A.Get (T);
end Prolog;
代码来自斯坦福大学的 Prolog 模块。 The project on GitHub

最佳答案

我不能说到底是什么导致了你的错误。这很可能是一个编译器错误。为了以微创的方式解决这个问题,我建议制作 access对通用单位不可见的关系:

procedure Prolog is

   generic
      type Item is limited private;
      type Link is private;
      with function Deref (L : Link) return Item is <>;
   package Garbage_Collector is
      procedure Get (L : in out Link) is null;
   end Garbage_Collector;

   type Node_Tag is (A, B);

   type Node (Tag : Node_Tag);
   type Term is access Node;

   type Node (Tag : Node_Tag) is
      record
         case Tag is
            when A => null;
            when B => null;
         end case;
      end record;

   subtype A_Node is Node (A);
   subtype A_Term is Term (A);
   
   function Deref (L : A_Term) return A_Node is (L.all);
   
   package Gc_A is new Garbage_Collector
     (Item  => A_Node,
      Link  => A_Term);

   T : Term;
begin
   Gc_A.Get (T);
end Prolog;
现在编译器不会提示因为正式类型 Link不再是访问类型并且与 Item 无关.通过给出函数 Deref ,您仍然可以取消引用 Link 类型的对象.如果您需要分配给它,您将需要一个附加功能。

关于generics - 实例化具有对变体记录的子类型访问权限的包时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67371096/

相关文章:

json - 在匹配 Go 中的底层类型时将 JSON 解码为接口(interface)变量

ada - out模式和in out模式的主要区别是什么?

ada - 是否可以声明 Range 类型的变量或常量?

ada - 数组元素过多仅编译器警告?

java - 如何在 Jackson 中为泛型创建自定义反序列化器?

Java内部类编译错误

Java 通配符与 API 中的对象

ada - Ada 中 protected 对象内部的访问类型

compiler-warnings - "requires body"警告

c# - Unity - 解析继承自 T 的泛型实现