The idea of code reuse arises from the necessity for constructing large software systems combining well-established building blocks. The reusability of code improves the productivity and the quality of software. The generic units are one of the ways in which the Ada language supports this characteristic. A generic unit is a subprogram or package that defines algorithms in terms of types and operations that are not defined until the user instantiates them.
Note to C++ programmers: generic units are similar to C++ templates.
For example, to define a procedure for swapping variables of any (non-limited) type:
generic
type Element_T is private;
procedure Swap (X, Y : in out Element_T);
procedure Swap (X, Y : in out Element_T) is
Temporary : constant Element_T := X;
begin
X := Y;
Y := Temporary;
end Swap;
The Swap subprogram is said to be generic. The subprogram specification is preceded by the generic formal part consisting of the reserved word generic followed by a list of generic formal parameters which may be empty. The entities declared as generic are not directly usable, it is necessary to instantiate them.
To be able to use Swap, it is necessary to create an instance for the wanted type. For example:
procedure Swap_Integers is new Swap (Integer);
Now the Swap_Integers procedure can be used for variables of type Integer.
The generic procedure can be instantiated for all the needed types. It can be instantiated with different names or, if the same identifier is used in the instantiation, each declaration overloads the procedure:
procedure Instance_Swap is new Swap (Float);
procedure Instance_Swap is new Swap (Day_T);
procedure Instance_Swap is new Swap (Element_T => Stack_T);
Similarly, generic packages can be used, for example, to implement a stack of any kind of elements:
generic
Max: Positive;
type Element_T is private;
package Generic_Stack is
procedure Push (E: Element_T);
function Pop return Element_T;
end Generic_Stack;
package body Generic_Stack is
Stack: array (1 .. Max) of Element_T;
Top : Integer range 0 .. Max := 0;
end Generic_Stack;
A stack of a given size and type could be defined in this way.
Source: Ada Programming
Related
A task unit is a program unit that is obeyed concurrently with the rest of an Ada program. The corresponding activity, a new locus of control, is
April 18, 2024
A render of a 6-cube. Source: Wikipedia
April 18, 2024
An ornamental design of five alternately interlaced pentagons (a kind of five-fold counterpart to the three-fold Valknut). No two pentagons are
April 18, 2024
In mathematics and especially in combinatorics, a plane partition is a two-dimensional array of nonnegative integers $ n_{i,j}} n_{{i,j}} $ (with
April 18, 2024
The idea of code reuse arises from the necessity for constructing large software systems combining well-established building blocks. The reusability
April 18, 2024
The Möbius strip or Möbius band is a surface with only one side and only one boundary. The Möbius strip has the mathematical property of being
April 18, 2024
In mathematics, the Klein bottle /ˈklaɪn/ is an example of a non-orientable surface; it is a two-dimensional manifold against which a system for
April 18, 2024
Among basic expressions are literals, for example, decimal (real), enumeration, string, and access value literals: Involving many of those one can
April 18, 2024
In geometry, a torus (plural tori) is a surface of revolution generated by revolving a circle in three-dimensional space about an axis coplanar with
April 18, 2024
A polygon discovered by Robert Amman, formed from a regular hexagon by adding projections on two of its sides and matching indentations on three
April 18, 2024
The generic unit declares generic formal parameters, which can be: objects (of mode in or in out but never out) types subprograms instances of
April 18, 2024
The Gaussian integers are [complex numbers] of the form , where u and v are ordinary [integers] and i is the [square root of negative one]. By
April 18, 2024
A monochrome render of a Truncated dodecahedron by Olfa Wektoryzacja, 2007. Source: Wikipedia
April 18, 2024
You can define a new type with the following syntax: followed by the description of the type, as explained in detail in each category of type.
April 18, 2024
around a saddle point. Selfmade with Mathematica and Inkscape. Source: Wikipedia
April 18, 2024
The mpl colormaps: magma, inferno, plasma and viridis. I previously attempted to implement these in the Cubehelix color space, but the originals are
April 18, 2024
This map shows the graph distance of each county from the Pacific or Atlantic coast; it is a recreation of a map posted to /r/dataisbeautiful using
April 18, 2024
Un des 9 groupes non-abéliens d’ordre 16 est le produit semi-direct de C_2^2 par C_4. On peut dessiner le graphe de Cayley de cette présentation sur
April 18, 2024
A plot of the bowl function Source: Wikipedia
April 18, 2024