D. Private types - JulTob/Ada GitHub Wiki

Private types

We can improve on the Buffer_type by limiting the access of upper clients.

To do so, we use packages. The declaration of a package, the file with .ads extension, determines the public entities that gain scope when called with with.

The private package and functionalities reside in the .adb file.

The Specifications also provide the Contract of use. Or Interface.

But for the user to be able to create variables of that type, it has to be defined in the interface.

So the compiler knows how much memory should allocate, it needs a declaration of types, even as private, in the interface. Therefore a tag is used to hide all private types from the user.

package Example is
   type Date is private;
...

private  --Here forward is all hidden.
      type Date is record
         Y,M,D:Integer;
      end record;  
end Example;

Private types still allow comparison and assignment. To deactivate these features for the user you should use the specification limited.

   type Date is limited private;

This limits the type to use inside the package. This is especially useful in outside resources out of the computer’s control.