1.3. Libraries - JulTob/Ada GitHub Wiki

Libraries, such as Simple_IO, are composed by two packages.

Specification, that describes the interface.

The Body, that contains the implementation.

We can define as many library modules as we want. We just have to make an interface file with all the public declarations and a body file with all the private algorithms and implementations.

[module_name].ads

package [module_name] is
...
end [module_name];

[module_name].adb

[Module’s libraries call]

package body [module_name] is
...
end [module_name];

We call the libraries from the body so the clients of this library are not clients of those libraries.

Using a Library is pleasantly simple. First off, ensure that it is in the same directory as the code that is trying to use the package. Then, simply using the with keyword, import its functionality. And then, you are free to call whichever function you want by the dot operator.

Example of Specifications:

Package Simple_IO is
   Procedure Get(F:    out Float);
   Procedure Put(F: in     Float);
   Procedure Put(S: in     String);
   Procedure New_Line(N: in     Integer := 1);
… 
end Simple_IO;

calling

Simple_IO.Get(f);

Parameter Specification

In enables de parameter to be used in the right side of an expression and to be read.

Out enables the parameter to be used in the left side of an assignment expression and to be overwritten.

They both can be combined in parameters that are both input and output of a process.

Implicitly, all parameters for Functions are input by default.

Also note the Overload specification for Put, allowing two types of inputs.

Last remark, the default value for New_Line as set.

Example of Body:

With Ada.Text_IO;
Package body Simple_IO is

   Procedure Get(F:    out Float) is
      ... --Algorithm goes here
   end Get;

... --Other processes

end Simple_IO;

The Body Package contains full procedures bodies and other supporting material needed for its implementation.

It’s hidden from the outside user.

Function

As procedures are described already, here how to declare and define functions.

Function Sqrt(F: Float) return Float;
--------------------------------------------
Function Sqrt(F: Float) return Float is
  R: Float;
begin 
   ...  -- Computation For Sqrt in the Reals
   return R;
end Sqrt;

With Header

The with clause shows that the implementation of the procedures in Simple_IO uses the more general package Ada.Text_IO. The notation indicates that Text_IO is a child package of the package Ada.

WITH Ada.Text_IO, Ada.Integer_Text_IO;
USE Ada.Text_IO, Ada.Integer_Text_IO;

Conclusion

One purpose of this section has been to stress that the idea of packages is one of the most important concepts in Ada. A program should be conceived as a number of components which provide services to and receive services from each other.