ada - 为什么前缀调用不适用于访问类型?

标签 ada

我在一个包中有一些方法可以操作一个访问常量到一个标记记录;为了调用这些函数,我必须指定包名。我宁愿只输入变量名 [dot] 函数名,但这会产生错误:no selector "foo"for type "Color"。这是为什么呢?

这是一个最小的复制器:

procedure Main is

  type Color is tagged
    record
      Hue : Integer;
      Saturation : Integer;
      Value : Integer;
    end record;

  type AccessColor is access constant Color;

  procedure foo (C : in AccessColor) is
  begin
    null;
  end foo;

  AccessS : AccessColor;

begin
  foo (AccessS);
  --AccessS.foo; -- does not work. Why?
end Main;

请注意,在我的真实代码中,完全指定函数是不方便的,因为与上面的示例不同,foo 是在单独的包中定义的:

Some.Package.Name.Depp.foo(AccessS);

尽管 AccessS 已经指定了在哪里找到函数,所以我应该能够做到:

AccessS.foo;

最佳答案

问题在于 foo 实际上不是 Color 的原始操作(在此再现器中)。

ARM 3.3.2(6)表示特定类型的原始子程序是

For a specific type declared immediately within a package_specification, any subprograms (in addition to the enumeration literals) that are explicitly declared immediately within the same package_specification and that operate on the type

这(对于重新格式化,大小写调整表示歉意)编译良好。

procedure Main is

   package Pak is

      type Color is tagged
         record
            Hue : Integer;
            Saturation : Integer;
            Value : Integer;
         end record;

      procedure Foo (C : in Color) is null;

      type AccessColor is access constant Color;

   end Pak;

   Col : aliased Pak.Color;

   AccessS : Pak.AccessColor := Col'Access;

begin
   AccessS.Foo;
end Main;

我将 Foo 声明为采用 in Color;如果需要,您同样可以声明它采用 access constant Color,因为 (ARM 4.1.3(9.2))

The first formal parameter of the subprogram shall be of type T, or a class-wide type that covers T, or an access parameter designating one of these types

关于ada - 为什么前缀调用不适用于访问类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57971198/

相关文章:

android - 为 Android 编译 Ada 库 (JNI)

eclipse - 如何将 ada 安装到 eclipse 中?

ada - 没有可用于程序/功能的全局契约(Contract)

exception - 如何在 Ada 中使用消息引发异常

tree - 在 Ada 中构建二叉表达式树

containers - 从有序记录集中删除元素 Ada

c - 如何在 CMake 文件中为 C/Ada 代码集成 gnatmake/gnatbind/gnatlink?

ada - 如何在 Ada 中存储(访问)Integer 的运算符?

debugging - 如何在 GDB 中打印类型属性?

standards - 仅在 ISO 标准 Ada 中,Record Representation Clause + 任何其他语言功能如何可移植到 little-endian 和 big-endian 处理器?