编译器不允许我获取字符串

The compiler won’t let me Get a string

一位用户最近发布了一个问题,并将其删除(可能是因为我们不太热情)。实际上,这就是问题所在:用 gnatmake -gnatwl person_1.adb 编译,结果是

     1. with Ada.Text_IO;                    use Ada.Text_IO;
     2. with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;
     3. procedure Person_1 is
     4.    type String is
     5.      array (Positive range <>) of Character;
     6.    S: String (1..10);
     7. begin
     8.    Put("Write a name: ");
     9.    Get(S);
           1   6
        >>> no candidate interpretations match the actuals:
        >>> missing argument for parameter "Item" in call to "Get" declared at a-tiinio.ads:90, instance at a-inteio.ads:18
        >>> missing argument for parameter "Item" in call to "Get" declared at a-tiinio.ads:51, instance at a-inteio.ads:18
        >>> missing argument for parameter "Item" in call to "Get" declared at a-textio.ads:451
        >>> missing argument for parameter "Item" in call to "Get" declared at a-textio.ads:378
        >>> expected type "Standard.Integer"
        >>> found type "String" defined at line 4
        >>>   ==> in call to "Get" at a-tiinio.ads:59, instance at a-inteio.ads:18
        >>>   ==> in call to "Get" at a-textio.ads:454
        >>>   ==> in call to "Get" at a-textio.ads:381

    10. end Person_1;

这很令人困惑。怎么回事?

麻烦的是,这段代码定义了自己的类型String,它隐藏了标准类型StringAda.Text_IO.Get 需要一个标准 String 类型的参数,但它实际上被赋予了一个本地 String 类型的参数。

Ada Wikibook 说,在要点下 Name Equivalence,

Two types are compatible if and only if they have the same name; not if they just happen to have the same size or bit representation. You can thus declare two integer types with the same ranges that are totally incompatible, or two record types with exactly the same components, but which are incompatible.

但是,这两种类型确实同名! (String)。不是吗?

毕竟,他们不这样做的原因是完全限定名称实际上是不同的。 Get 期望 Standard.String (ARM A.1(37)),但本地版本是 Person_1.String.

您可能希望 -gnatwh(打开隐藏声明的警告)会报告此问题,但不幸的是没有。

我不确定为什么编译器只报告 Ada.Integer_Text_IO 内的失败匹配(with Integer);如果我们删除这个 withuse(毕竟它没有被使用),事情就会变得更有帮助,

     1. with Ada.Text_IO;                    use Ada.Text_IO;
     2. --  with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;
     3. procedure Person_1 is
     4.    type String is
     5.      array (Positive range <>) of Character;
     6.    S: String (1..10);
     7. begin
     8.    Put("Write a name: ");
     9.    Get(S);
           1   4
        >>> no candidate interpretations match the actuals:
        >>> missing argument for parameter "Item" in call to "Get" declared at a-textio.ads:451
        >>> missing argument for parameter "Item" in call to "Get" declared at a-textio.ads:378
        >>> expected type "Standard.String"
        >>> found type "String" defined at line 4
        >>>   ==> in call to "Get" at a-textio.ads:454
        >>>   ==> in call to "Get" at a-textio.ads:381

    10. end Person_1;