【发布时间】:2018-02-23 21:10:13
【问题描述】:
我将从 Ada 中泛型过程的经典示例开始:
-------------------------
-- swaps.ads
-------------------------
package Swaps is
generic
type E is private;
procedure Generic_Swap (Left, Right : in out E);
end Swaps;
-------------------------
-- swaps.adb
-------------------------
package body Swaps is
procedure Generic_Swap (Left, Right : in out E) is
Temporary : E;
begin
Temporary := Left;
Left := Right;
Right := Temporary;
end Generic_Swap;
end Swaps;
现在假设我想实现一个专门的String_Swap 过程来交换字符串,并将它提供给我包的所有用户。我可以在swaps.adb 的正文声明中添加以下内容:
procedure String_Swap is new Generic_Swap (String);
但是,如果我在swaps.ads 中的规范中没有添加任何内容,则没有包可以使用此过程。例如:
-------------------------
-- main.adb
-------------------------
with Swaps; use Swaps;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
First : String := "world!";
Second : String := "Hello, ";
begin
String_Swap (First, Second); -- #Error: String_Swap is undefined#
Put_Line (First);
Put_Line (Second);
end Main;
我已尝试将程序的类型添加到规范中:
procedure String_Swap (Left, Right : in out String);
但随后 Ada 抱怨此规范缺少正文,并且 swaps.adb 中的定义与它冲突。
【问题讨论】: