ada Packages More on Packages


Example

In the Hello World, you were introduced to the package Ada.Text_IO, and how to use it in order to perform I/O operations within your program. Packages can be further manipulated to do many different things.

Renaming: To rename a package, you use the keyword renames in a package declaration, as such:

package IO renames Ada.Text_IO;

Now, with the new name, you can use the same dotted notation for functions like Put_Line (i.e. IO.Put_Line), or you can just use it with use IO. Of course, saying use IO or IO.Put_Line will use the functions from the package Ada.Text_IO.


Visibility & Isolation: In the Hello World example we included the Ada.Text_IO package using a with clause. But we also declared that we wanted to use Ada.Text_IO on the same line. The use Ada.Text_IO declaration could have been moved into the declarative part of the procedure:

with Ada.Text_IO;
 
procedure hello_world is
   use Ada.Text_IO;
begin
   Put_Line ("Hello, world!");
end hello_world;

In this version, the procedures, functions, and types of Ada.Text_IO are directly available inside the procedure. Outside the block in which use Ada.Text_IO is declared, we would have to use the dotted notation to invoke, for example:

with Ada.Text_IO;
 
procedure hello_world is
begin
   Ada.Text_IO.Put ("Hello, ");       --  The Put function is not directly visible here
   declare
      use Ada.Text_IO;
   begin
      Put_Line ("world!");            --  But here Put_Line is, so no Ada.Text_IO. is needed
   end;
end hello_world;

This enables us to isolate the use … declarations to where they are necessary.