12.6 Formal Subprograms
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
[ Formal subprograms can be used to pass callable entities to a generic unit.]
Language Design Principles
explicit_generic_actual_parameter
. Syntax
2/2formal_subprogram_declaration
::=
formal_concrete_subprogram_declaration
| formal_abstract_subprogram_declaration
2.1/3formal_concrete_subprogram_declaration
::=
with subprogram_specification
[is subprogram_default
]
[aspect_specification
];
2.2/3formal_abstract_subprogram_declaration
::=
with subprogram_specification
is abstract [subprogram_default
]
[aspect_specification
];
3/2subprogram_default
::=
default_name
| <> | null
4default_name
::=
name
4.1/2A subprogram_default
of null shall not be specified for a formal function or for a formal_abstract_subprogram_declaration
.
Name Resolution Rules
5The expected profile for the default_name
, if any, is that of the formal subprogram.
generic_declaration
.default_name
takes place during the elaboration of each instantiation that uses the default, as defined in 12.3, “Generic Instantiation”. For a generic formal subprogram, the expected profile for the actual is that of the formal subprogram.
Legality Rules
7/3The profiles of the formal and any named default shall be mode conformant.
generic_declaration
. The profiles of the formal and actual shall be mode conformant.
For a parameter or result subtype of a formal_subprogram_declaration
that has an explicit null_exclusion
:
- if the actual matching the
formal_subprogram_declaration
statically denotes a generic formal subprogram of another generic unit G, and the instantiation containing the actual occurs within the body of a generic unit G or within the body of a generic unit declared within the declarative region of the generic unit G, then the corresponding parameter or result type of the formal subprogram of G shall have anull_exclusion
; 8.3/2 - otherwise, the subtype of the corresponding parameter or result type of the actual matching the
formal_subprogram_declaration
shall exclude null. In addition to the places where Legality Rules normally apply (see 12.3), this rule applies also in the private part of an instance of a generic unit.
null_exclusion
. The first bullet is an assume-the-worst rule which prevents trouble in generic bodies (including bodies of child generics) when the formal subtype excludes null implicitly. If the named default, if any, is a prefixed view, the prefix of that view shall denote an object for which renaming is allowed (see 8.5.1). Similarly, if the actual subprogram in an instantiation is a prefixed view, the prefix of that view shall denote an object for which renaming is allowed.
If a formal parameter of a formal_abstract_subprogram_declaration
is of a specific tagged type T or of an anonymous access type designating a specific tagged type T, T is called a controlling type of the formal_abstract_subprogram_declaration
. Similarly, if the result of a formal_abstract_subprogram_declaration
for a function is of a specific tagged type T or of an anonymous access type designating a specific tagged type T, T is called a controlling type of the formal_abstract_subprogram_declaration
. A formal_abstract_subprogram_declaration
shall have exactly one controlling type, and that type shall not be incomplete.
formal_abstract_subprogram_declaration
an abstract formal subprogram, but we do not use this term in normative wording. (We do use it often in these notes.) The actual subprogram for a formal_abstract_subprogram_declaration
shall be:
- a dispatching operation of the controlling type; or
- if the controlling type is a formal type, and the actual type corresponding to that formal type is a specific type T, a dispatching operation of type T; or
- if the controlling type is a formal type, and the actual type is a class-wide type T'Class, an implicitly declared subprogram corresponding to a primitive operation of type T (see Static Semantics below).
formal_abstract_subprogram_declaration
, of course. Saying that gets unwieldy and redundant (so says at least one reviewer, anyway). generic
type T(<>) is tagged private;
with procedure Foo (Obj : in T) is abstract;
package P ...
8.h/2package New_P is new P (Something'Class, Some_Proc);
generic
type NT(<>) is new T with private;
-- Presume that T has the following primitive operation:
-- procedure Bar (Obj : in T);
package Gr ...
8.l/2package body Gr is
package New_P2 is new P (NT, Foo => Bar);
end Gr;
8.m/2package New_Gr is new Gr (Something'Class);
Static Semantics
9A formal_subprogram_declaration
declares a generic formal subprogram. The types of the formal parameters and result, if any, of the formal subprogram are those determined by the subtype_mark
s given in the formal_subprogram_declaration
; however, independent of the particular subtypes that are denoted by the subtype_mark
s, the nominal subtypes of the formal parameters and result, if any, are defined to be nonstatic, and unconstrained if of an array type [(no applicable index constraint is provided in a call on a formal subprogram)]. In an instance, a formal_subprogram_declaration
declares a view of the actual. The profile of this view takes its subtypes and calling convention from the original profile of the actual entity, while taking the formal parameter name
s and default_expression
s from the profile given in the formal_subprogram_declaration
. The view is a function or procedure, never an entry.
formal_subprogram_declaration
is analogous to a renaming-as-declaration, and the actual is analogous to the renamed view. If a subtype_mark
in the profile of the formal_subprogram_declaration
denotes a formal private or formal derived type and the actual type for this formal type is a class-wide type T'Class, then for the purposes of resolving the corresponding actual subprogram at the point of the instantiation, certain implicit declarations may be available as possible resolutions as follows:
For each primitive subprogram of T that is directly visible at the point of the instantiation, and that has at least one controlling formal parameter, a corresponding implicitly declared subprogram with the same defining name, and having the same profile as the primitive subprogram except that T is systematically replaced by T'Class in the types of its profile, is potentially use-visible. The body of such a subprogram is as defined in 12.5.1 for primitive subprograms of a formal type when the actual type is class-wide.
If a generic unit has a subprogram_default
specified by a box, and the corresponding actual parameter is omitted, then it is equivalent to an explicit actual parameter that is a usage name identical to the defining name of the formal.
If a generic unit has a subprogram_default
specified by the reserved word null, and the corresponding actual parameter is omitted, then it is equivalent to an explicit actual parameter that is a null procedure having the profile given in the formal_subprogram_declaration
.
The subprogram declared by a formal_abstract_subprogram_declaration
with a controlling type T is a dispatching operation of type T.
subprogram_renaming_declaration
s (see 8.5.4). In particular, the name of a parameter of the formal subprogram can be different from that of the corresponding parameter of the actual subprogram; similarly, for these parameters, default_expression
s can be different .subtype_mark
in the _specification
of the formal subprogram). A similar remark applies to the result of a function. Therefore, to avoid confusion, it is recommended that the name
of a first subtype be used in any declaration of a formal subprogram.generic_formal_part
.default_name
denotes an entity that is visible or directly visible at the place of the generic_declaration
; a box used as a default is equivalent to a name that denotes an entity that is directly visible at the place of the generic_instantiation
. formal_abstract_subprogram_declaration
(see 3.9.3).formal_abstract_subprogram_declaration
is an abstract subprogram. All calls on a subprogram declared by a formal_abstract_subprogram_declaration
are limited to dispatching calls. See 3.9.3.Examples
17Examples of generic formal subprograms:
with function "+"(X, Y : Item) return Item is <>;
with function Image(X : Enum) return String is Enum'Image;
with procedure Update is Default_Update;
with procedure Pre_Action(X : in Item) is null; -- defaults to no action
with procedure Write(S : not null access Root_Stream_Type'Class;
Desc : Descriptor)
is abstract Descriptor'Write; -- see 13.13.2
-- Dispatching operation on Descriptor with default
19-- given the generic procedure declaration
20generic
with procedure Action (X : in Item);
procedure Iterate(Seq : in Item_Sequence);
21-- and the procedure
22procedure Put_Item(X : in Item);
23-- the following instantiation is possible
24procedure Put_List is new Iterate(Action => Put_Item);
Extensions to Ada 95
formal_abstract_subprogram_declaration
is new. It allows the passing of dispatching operations to generic units.Wording Changes from Ada 95
Incompatibilities With Ada 2005
Extensions to Ada 2005
aspect_specification
can be used in a formal_concrete_subprogram_declaration
and a formal_abstract_subprogram_declaration
. This is described in 13.1.1. Incompatibilities With Ada 2012
Extensions to Ada 2012
formal_abstract_subprogram_declaration
to be an implicitly declared subprogram for a class-wide type. The rules already required a compiler to be able to construct this subprogram for use in an instantiation that occurs inside of a generic unit, so it made no sense to prevent it from being used explicitly as well (in the same way that the previous extension allows them to be used for other kinds of formal subprograms).