Three main classes of entity are found in Ada programs:
For each class of entity, the language provides a copying mechanism; that is, a mechanism by which we can create distinct replicas having similar properties:
class of entity | replication mechanism | ||
---|---|---|---|
|
|
Clearly, there are limits to this analogy since each replication mechanism is adapted to the class of entities to which it applies.
The replication mechanism offered by generic units is very powerful and can be used to replicate the contents of program units. Thus in section 7.3 it was shown that some aspects of type replication (not all, however) could be achieved by generic instantiation of a package that included the model type.
The generality of generic units certainly runs against a saying often heard in programming language design that there should be only one way of doing a given thing. Generic instantiation can even be used to achieve object replication, but it would be carrying matters to extremes to conclude from this that no simpler way should be provided. Consider thus
generic package CREATE is OBJECT : INTEGER; end; |
and now we can replicate objects by generic instantiation:
package A is new CREATE; -- creates A.OBJECT package B is new CREATE; -- creates B.OBJECT ... package Z is new CREATE; -- creates Z.OBJECT |
But (obviously) replication of objects could be achieved in a simpler manner:
A : INTEGER; B : INTEGER; ... Z : INTEGER; |
The awkwardness of the above example should be an indication of the limitations of the only one way principle. (Note also that we are in an inescapable circular situation since both ways imply an object declaration.) Generic instantiation can be used to replicate objects but it is not the most natural way. Similarly, although generic instantiation can be used to replicate types, derivation is a more natural and direct way.
To summarize this point, the design of Ada has provided replication mechanisms for each of the three main classes of entity: objects, types, and program units. Each of these mechanisms is adapted to the corresponding class of entity: it provides the natural way of replicating the corresponding entities. Being specialized, these mechanisms can also take advantage of specific aspects of the entities concerned. In the case of generic units these specific aspects correspond to parameterization. In the case of derived types the specific aspects correspond to the allowed explicit conversions.
Six major situations have been reviewed in which copying a type provides a natural solution for the problem considered: