我尝试使用 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_Term
至 type 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/