当尝试获取无界字符串的第一个或最后一个索引时,如下程序所示
with Ada.Strings; use Ada.Strings; -- for `Backward`
with Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
procedure Foo is
package U_Str renames Ada.Strings.Unbounded;
S : U_Str.Unbounded_String := U_Str.To_Unbounded_String ("example unbounded string");
I : Natural := U_Str.Index (Source => S,
Pattern => "s",
Going => Backward,
From => S'Last);
begin
Put_Line (I'Image);
end Foo;
GNAT 抱怨U_Str.Unbounded_String
自己是私有类型:
$ gnatmake foo.adb
x86_64-linux-gnu-gcc-10 -c foo.adb
foo.adb:12:41: prefix for "Last" attribute may not be private type
gnatmake: "foo.adb" compilation error
我不明白这一点,同样的程序用常规方法String
可以正常工作:
with Ada.Strings; use Ada.Strings; -- for `Backward`
with Ada.Strings.Fixed;
with Ada.Text_IO; use Ada.Text_IO;
procedure Foo is
package F_Str renames Ada.Strings.Fixed;
S : String := "example fixed string";
I : Natural := F_Str.Index (Source => S,
Pattern => "s",
Going => Backward,
From => S'Last);
begin
Put_Line (I'Image);
end Foo;
$ gnatmake foo.adb
x86_64-linux-gnu-gcc-10 -c foo.adb
x86_64-linux-gnu-gnatbind-10 -x foo.ali
x86_64-linux-gnu-gnatlink-10 foo.ali
$ ./foo
15
无界字符串没有'First
和'Last
属性吗?Ada 参考手册只说明
下列属性是针对前缀 A 定义的,该前缀 A 属于数组类型(在任何隐式取消引用之后),或表示受约束的数组子类型
但它没有提到任何有关私有类型的信息。这是 GNAT 错误还是我遗漏了什么?