1

发帖有两个原因:(1)对于这样一个简单的问题,我被困在无用的编译器错误上太久了,我希望下一个人在谷歌上搜索这些消息以找到我(或其他)的答案,以及(2)我仍然不明白不允许使用子句,所以我自己的答案确实不完整。

为了在两个地方使用几乎相同的参数调用程序,我想使用 '&' 附加到默认的内联列表:

declare
   Exit_Code : constant Integer := GNAT.OS_Lib.Spawn (Program_Name => "gprbuild", Args => (Default_GPR_Arguments & new String'(File_Name_Parameter)));
begin
   if Exit_Code /= 0 then
      raise Program_Error with "Exit code:" & Exit_Code'Image;
   end if;
end;

但是,编译器抱怨System.Strings.String_List需要一个 use 子句:

operator for type "System.Strings.String_List" is not directly visible
use clause would make operation legal

但是插入会use System.Strings.String_List产生:

"System.Strings.String_List" is not allowed in a use clause

我也收到了这个警告:

warning: "System.Strings" is an internal GNAT unit
warning: use "GNAT.Strings" instead

因此,我在 with 和 use 子句中用 GNAT 代替了 System,除了原来的“您需要 System.Strings.String_List 的 use 子句”之外,还得到了一个额外的错误:

"GNAT.Strings.String_List" is not allowed in a use clause

为什么 GNAT.Strings.String_List 不允许在 use 子句中使用?关于 use 子句的第 8.5 节似乎没有对不允许的包说明任何内容,所以这是编译器错误吗?是否可以定义一个不能有 use 子句的新包?

4

2 回答 2

3

在形式的使用子句中

use Name;

Name必须是包名。GNAT.Strings.String_List是子类型名称,而不是包名称。

有多种调用"&"for的方法String_List。最简单的就是使用全名:

GNAT.Strings."&" (Left, Right)

但大概您希望能够将其用作中缀表示法的运算符,Left & Right. 在降低特异性的情况下实现这一目标的方法:

  • function "&" (Left : GNAT.Strings.String_List; Right : GNAT.Strings.String_List) return GNAT.Strings.String_List renames GNAT.Strings."&";这使得该特定功能直接可见。
  • use type GNAT.Strings.String_List;这使得该类型的所有原始运算符直接可见。
  • use all type GNAT.Strings.String_List;这使得该类型的所有原始操作(包括非运算符操作)直接可见。
  • use GNAT.Strings;这使得包中的所有内容都直接可见。
于 2020-08-28T09:33:05.453 回答
0

Looks like it is a design decision. And many other packages in System follows this rule. From the s-string.ads (package specification for System.String):

--  Note: this package is in the System hierarchy so that it can be directly
--  be used by other predefined packages. User access to this package is via
--  a renaming of this package in GNAT.String (file g-string.ads).

My guess why this is done in that way: because it isn't in the Ada specification, but extension from GNAT.

于 2020-08-27T07:56:31.013 回答