package
identifier is
new
generic_package_name [generic_actual_part];
| procedure
identifier is
new
generic_procedure_name [generic_actual_part];
| function
designator is
new
generic_function_name [generic_actual_part];
generic_actual_part ::=
(generic_association {, generic_association})
[generic_formal_parameter =>] generic_actual_parameter
| subprogram_name | entry_name | type_mark
in
; a variable name can match a formal object of mode in out
; a subprogram name or an entry name can match a formal subprogram; a type mark can match a formal type. The detailed rules defining the allowed matches are given in sections 12.3.1 to 12.3.6; these are the only allowed matches.
in
: The corresponding name denotes a constant whose value is a copy of the value of the associated generic actual parameter.
in out
: The corresponding name denotes the variable named by the associated generic actual parameter.
procedure
SWAP is new
EXCHANGE(ELEM => INTEGER); procedure
SWAP is new
EXCHANGE(CHARACTER); -- SWAP is overloaded function
SQUARE is new
SQUARING(INTEGER);
-- "*" of INTEGER used by default
function
SQUARE is new
SQUARING
(ITEM => MATRIX, "*" => MATRIX_PRODUCT);
function
SQUARE is new
SQUARING(MATRIX, MATRIX_PRODUCT);
-- same as previous
package
INT_VECTORS is new
ON_VECTORS(INTEGER, TABLE, "+");
SWAP(A, B);
A := SQUARE(A);
N : INTEGER := INT_VECTORS.SIGMA(T); -- 150 (see 12.2 for the body
-- of SIGMA)
use
INT_VECTORS; M : INTEGER := SIGMA(T); -- 150
generic
type
A is
(<>);
type
B is private
;
package
G is
function
NEXT(X : A) return
A;
function
NEXT(X : B) return
B;
end
; package
P is new
G(A => BOOLEAN, B => BOOLEAN);
-- calls of P.NEXT are ambiguous
in
of a given type is matched by an expression of the same type. If a generic unit has a generic formal object of mode in, a check is made that the value of the expression belongs to the subtype denoted by the type mark, as for an explicit constant declaration (see 3.2.1). The exception CONSTRAINT_ERROR is raised if this check fails.
in out
of a given type is matched by the name of a variable of the same type. The variable must not be a formal parameter of mode out
or a subcomponent thereof. The name must denote a variable for which renaming is allowed (see 8.5).
in
must not be a limited type. The constraints that apply to a generic formal parameter of mode in out
are those of the corresponding generic actual parameter (see 12.1.1).
range
<> is matched by any integer subtype. A generic formal type defined by digits
<> is matched by any floating point subtype. A generic formal type defined by delta
<> is matched by any fixed point subtype. No other matches are possible for these generic formal types.
-- given the generic package
generic
type
ITEM is private
;
type
INDEX is
(<>);
type
VECTOR is array
(INDEX range
<>) of
ITEM;
type
TABLE is array
(INDEX) of
ITEM;
package
P is
...
end
;
type
MIX is array
(COLOR range
<>) of
BOOLEAN;
type
OPTION is array
(COLOR) of
BOOLEAN;
package
R is new
P(ITEM => BOOLEAN, INDEX => COLOR,
VECTOR => MIX, TABLE => OPTION);
-- the formal types of the generic package
generic
type
NODE is private
;
type
LINK is access
NODE;
package
P is
...
end
;
type
CAR;
type
CAR_NAME is access
CAR;
type
CAR is
record
PRED, SUCC : CAR_NAME;
NUMBER : LICENSE_NUMBER;
OWNER : PERSON;
end record
; package
R is new
P(NODE => CAR, LINK => CAR_NAME);
-- given the generic function specification
generic
type
ITEM is private
;
with
function
"*" (U, V : ITEM) return
ITEM is
<>;
function
SQUARING(X : ITEM) return
ITEM;
function
MATRIX_PRODUCT(A, B : MATRIX) return
MATRIX;
function
SQUARE is new
SQUARING(MATRIX, MATRIX_PRODUCT);
function
SQUARE is new
SQUARING(ITEM => INTEGER, "*" => "*");
function
SQUARE is new
SQUARING(INTEGER, "*");
function
SQUARE is new
SQUARING(INTEGER);