'外部包

时间:2018-01-09 18:05:33

标签: generics ada

我在Ada中遇到了一些泛型问题。鉴于以下示例,gnatmake导致:

g_package.adb:13:33: 'Access attribute not allowed in generic body
g_package.adb:13:33: because access type "t_callback_p" is declared outside generic unit (RM 3.10.2(32))
g_package.adb:13:33: move 'Access to private part, or (Ada 2005) use anonymous access type instead of "t_callback_p"
gnatmake: "g_package.adb" compilation error

假设无法更改外部包,有没有办法解决这个问题?我首先得到了错误消息的原因(编译器不知道通用包的正确类型,但是当传递的函数没有触及泛型的任何部分时,它有点烦人。)< / p>

g_package.adb

with Ada.Text_IO; use Ada.Text_IO;
with external;

package body g_package is
   procedure quix (f : String) is
   begin
      Put_Line ("Does a thing");
   end quix;

   procedure foo (bar : String) is
   begin
      Put_Line ("baz" & bar & Boolean'Image(flag));
      external.procedure_f (quix'Access);
   end foo;
end g_package;

g_package.ads

generic
   flag : Boolean;
package g_package is
   procedure foo (bar : String);
end g_package;

external.ads

package external is
   type t_callback_p is access procedure (s : String);
   procedure procedure_f (proc : t_callback_p);
end external;

2 个答案:

答案 0 :(得分:5)

您可以(如错误消息中所述)移动&#39;访问包规范的私有部分:

private with external;

generic
   flag : Boolean;
package g_package is
   procedure foo (bar : String);

private
   procedure quix (f : String);
   quix_access : constant external.t_callback_p := quix'Access;

end g_package;

并使用身体中的常量:

external.procedure_f (quix_access);

答案 1 :(得分:0)

Ada无法做到这一点。如果您希望不可移植并且正在使用(如图所示)GNAT,则可以使用特定于GNAT的属性'Unrestricted_Access