Ada Reference Manual (Ada 2022) — Legal Information

# 12.5 Formal Types

1/2
A generic formal subtype can be used to pass to a generic unit a subtype whose type is in a certain category of types.

#### Syntax

2/3
formal_type_declaration ::=
formal_complete_type_declaration
| formal_incomplete_type_declaration
2.1/5
formal_complete_type_declaration ::=
type defining_identifier[discriminant_part] is formal_type_definition
[or use default_subtype_mark] [aspect_specification];
2.2/5
formal_incomplete_type_declaration ::=
type defining_identifier[discriminant_part] [is tagged]
[or use default_subtype_mark];
3/2
formal_type_definition ::=
formal_private_type_definition
| formal_derived_type_definition
| formal_discrete_type_definition
| formal_signed_integer_type_definition
| formal_modular_type_definition
| formal_floating_point_definition
| formal_ordinary_fixed_point_definition
| formal_decimal_fixed_point_definition
| formal_array_type_definition
| formal_access_type_definition
| formal_interface_type_definition

#### Legality Rules

4
For a generic formal subtype, the actual shall be a subtype_mark; it denotes the (generic) actual subtype.

#### Static Semantics

5
A formal_type_declaration declares a (generic) formal type, and its first subtype, the (generic) formal subtype.
6/3
The form of a formal_type_definition determines a category (of types) to which the formal type belongs. For a formal_private_type_definition the reserved words tagged and limited indicate the category of types (see 12.5.1). The reserved word tagged also plays this role in the case of a formal_incomplete_type_declaration. For a formal_derived_type_definition the category of types is the derivation class rooted at the ancestor type. For other formal types, the name of the syntactic category indicates the category of types; a formal_discrete_type_definition defines a discrete type, and so on.

#### Legality Rules

7/2
The actual type shall be in the category determined for the formal.
7.1/5
The default_subtype_mark, if any, shall denote a subtype which is allowed as an actual subtype for the formal type.

#### Static Semantics

8/5
The formal type also belongs to each category that contains the determined category. The primitive subprograms of the type are as for any type in the determined category. For a formal type other than a formal derived type, these are the predefined operators of the type. For an elementary formal type, the predefined operators are implicitly declared immediately after the declaration of the formal type. For a composite formal type, the predefined operators are implicitly declared either immediately after the declaration of the formal type, or later immediately within the declarative region in which the type is declared according to the rules of 7.3.1. In an instance, the copy of such an implicit declaration declares a view of the predefined operator of the actual type, even if this operator has been overridden for the actual type and even if it is never declared for the actual type, unless the actual type is an untagged record type, in which case it declares a view of the primitive (equality) operator. The rules specific to formal derived types are given in 12.5.1.
9
NOTE 1 Generic formal types, like all types, are not named. Instead, a name can denote a generic formal subtype. Within a generic unit, a generic formal type is considered as being distinct from all other (formal or nonformal) types.
10
NOTE 2 A discriminant_part is allowed only for certain kinds of types, and therefore only for certain kinds of generic formal types. See 3.7.

#### Examples

11
Examples of generic formal types:
12
type Item is private;
type Buffer(Length : Natural) is limited private;
13
type Enum is (<>);
type Int is range <>;
type Angle is delta <>;
type Mass is digits <>;
14
type Table is array (Enum) of Item;
15
Example of a generic formal part declaring a formal integer type:
16
generic
type Rank is range <>;
First : Rank := Rank'First;
Second : Rank := First + 1; -- the operator "+" of the type Rank

Ada 2005 and 2012 Editions sponsored in part by Ada-Europe