I'll start with the classic example of a generic procedure in 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;
Now suppose I want to implement a specialized String_Swap
procedure for swapping strings, and provide it to all users of my package. I can add the following to the body declaration in swaps.adb
:
procedure String_Swap is new Generic_Swap (String);
However, if I add nothing to the specification in swaps.ads
, then no package can use this procedure. For example:
-------------------------
-- 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;
I've tried to add the type of the procedure in to the specification:
procedure String_Swap (Left, Right : in out String);
but then Ada complains that this specification has a missing body and that the definition in swaps.adb
conflicts with it.