12.7 Formal Packages
packages can be used to pass packages to a generic unit. The formal_package_declaration
declares that the formal package is an instance of a given generic package.
Upon instantiation, the actual package has to be an instance of that
shall denote a generic package (the template
for the formal package);
the formal package is an instance of the template.
The above rule is simple to state, though it does not reflect the fact
that the formal package functions like an instantiation of a special
kind, where each box association for a generic_formal_parameter_declaration
is replaced with a new entity F
' that has the same characteristics
: if F
is a formal discrete type then F
a discrete type, if F
is a formal subprogram then F
a subprogram with a similar signature, etc. In practice this is achieved
by making the association into a copy of the declaration of the generic
The actual shall be an instance of the template. If the formal_package_actual_part
is (<>) or (others
=> <>), [then the actual may
be any instance of the template]; otherwise, certain of the actual parameters
of the actual instance shall match the corresponding actual parameters
of the formal package, determined as follows:
Otherwise, all actual parameters shall match[, whether any actual parameter
is given explicitly or by default].
The rules for matching of actual parameters between the actual instance
and the formal package are as follows:
For a formal object of mode in
, the actuals match if they are
static expressions with the same value, or if they statically denote
the same constant, or if they are both the literal null
Reason: We can't simply require full
conformance between the two actual parameter expressions, because the
two expressions are being evaluated at different times.
For a formal subtype, the actuals match if they
denote statically matching subtypes.
For other kinds of formals, the actuals match if
they statically denote the same entity.
For the purposes of matching, any actual parameter that is the name of
a formal object of mode in
is replaced by the formal object's
actual expression (recursively).
The visible part of a formal package includes the
first list of basic_declarative_item
of the package_specification
In addition, for each actual parameter that is not required to match,
a copy of the declaration of the corresponding formal parameter of the
template is included in the visible part of the formal package. If the
copied declaration is for a formal type, copies of the implicit declarations
of the primitive subprograms of the formal type are also included in
the visible part of the formal package.
If the formal_package_actual_part
is (<>), then the declarations that occur immediately within the
of the template for the formal package are visible outside the formal
package, and can be denoted by expanded names outside the formal package.If
only some of the actual parameters are given by <>, then the declaration
corresponding to those parameters (but not the others) are made visible.
We always want either the actuals or the formals of an instance to be
nameable from outside, but never both. If both were nameable, one would
get some funny anomalies since they denote the same entity, but, in the
case of types at least, they might have different and inconsistent sets
of primitive operators due to predefined operator “reemergence.”
Formal derived types exacerbate the difference. We want the implicit
declarations of the generic_formal_part
as well as the explicit declarations, so we get operations on the formal
Ramification: A generic formal package
is a package, and is an instance. Hence, it is possible to pass a generic
formal package as an actual to another generic formal package.
For the purposes of matching, if the actual instance A
a formal package, then the actual parameters of A
are those specified
explicitly or implicitly in the formal_package_actual_part
, plus, for those not specified, the copies of the formal
parameters of the template included in the visible part of A
Example of a generic package with formal package parameters:
Ada.Containers.Ordered_Maps; -- see A.18.6
Mapping_1 is new
Mapping_2 is new
(Key_Type => Mapping_1.Element_Type,
-- Provide a "join" between two mappings
subtype Key_Type is Mapping_1.Key_Type;
subtype Element_Type is Mapping_2.Element_Type;
function Lookup(Key : Key_Type) return Element_Type;
Example of an instantiation of a package with formal packages:
package Symbol_Package is
subtype Key_String is String(1..5);
type Symbol_Info is ...
String_Table is new
(Key_Type => Key_String String
Element_Type => String_Id);
package Symbol_Table is new Ada.Containers.Ordered_Maps
(Key_Type => String_Id,
Element_Type => Symbol_Info);
package String_Info is new Ordered_Join(Mapping_1 => String_Table,
Mapping_2 => Symbol_Table);
Apple_Info : constant Symbol_Info := String_Info.Lookup("Apple");
Extensions to Ada 83
Formal packages are new
to Ada 95.
Extensions to Ada 95
It's now allowed to mix actuals of a formal package
that are specified with those that are not specified.
Wording Changes from Ada 95
Corrected the description of formal package matching
to say that formal parameters are always replaced by their actual parameters
(recursively). This matches the actual practice of compilers, as the
ACATS has always required this behavior.
The description of which operations are visible in a formal package has
been clarified. We also specify how matching is done when the actual
is a formal package.
Incompatibilities With Ada 2005
Added missing rules for parameters
of generic formal package that parallel those in 12.3
as well as some specific to <> parameters. These are technically
incompatibilities because generic formal package parameters that Ada
95 and Ada 2005 would have considered legal now have to be rejected.
But this should not be an issue in practice as such formal parameters
could not have matched any actual generics. And it is quite likely that
implementations already enforce some of these rules.
Extensions to Ada 2005
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe