13.1 Generic procedures
A generic procedure is different from a traditional procedure, it can't be called, but it can be instantiated and to be able to use in different applications. Here is an example of a generic procedure for an indicator of a fuel tank and it is weakly reusable.
generic -- generic procedure for Display_Fuel
type Reading is float;
---------------
procedure Display_Fuel ( Panel : in out Reading);
procedure Display_Fuel ( Panel : in out Reading) is
-------------------------------------------------
end Display_Fuel;
In order to modify this into a strongly reusable component, we need to adapt the following guidelines.
Guideline1: Provide generic formal subprograms or parameters in order to deal with the initial and final
boundary conditions. But balance must be made between number
of generic formal procedures or parameters and information hiding
principle.
The above procedure can be re-written as follow.
generic -- procedure Display_Fuel
type Reading is digits (<>);
type Display is private;
with function Default return Reading; -- generic formal procedure
with procedure Initial ( Tank : in out Reading);
with procedure Increase ( previous : in Reading; New : in out Reading);
with procedure Final ( Tank : in out Reading);
procedure Display_Fuel ( Tank : in Reading; Panel : in out Display);
procedure Display_Fuel ( Tank : in Reading; Panel : in out Display) is
--------------------------
end Display_Fuel;
Guideline2 : Generic procedures can be instantiated by supplying actual
parameters as shown below.
procedure Swap is new Exchange ( Elem => integer );
It can be called as follow.
Swap ( A, B );