我在一个包中有一些方法可以操作一个访问常量到一个标记记录;为了调用这些函数,我必须指定包名。我宁愿只输入变量名 [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/