____ | Up | Next | Back | Forward
TOC -- / --.-- / --.--.-- | Index | Search | Syntax | Help


12.1 Generic Declarations

(1)
A generic_declaration declares a generic unit, which is either a generic subprogram or a generic package. A generic_declaration includes a generic_formal_part declaring any generic formal parameters. A generic formal parameter can be an object; alternatively (unlike a parameter of a subprogram), it can be a type, a subprogram, or a package.
Syntax
(2)
       generic_declaration ::= generic_subprogram_declaration | generic_package_declaration
(3)
       generic_subprogram_declaration ::=
            generic_formal_part  subprogram_specification;
(4)
       generic_package_declaration ::=
            generic_formal_part  package_specification;
(5)
       generic_formal_part ::= generic {generic_formal_parameter_declaration | use_clause}
(6)
       generic_formal_parameter_declaration ::=
             formal_object_declaration
           | formal_type_declaration
           | formal_subprogram_declaration
           | formal_package_declaration
(7)
Static Semantics
(8)
A generic_declaration declares a generic unit -- a generic package, generic procedure or generic function, as appropriate.
(9)
An entity is a generic formal entity if it is declared by a generic_formal_parameter_declaration. ``Generic formal,'' or simply ``formal,'' is used as a prefix in referring to objects, subtypes (and types), functions, procedures and packages, that are generic formal entities, as well as to their respective declarations. Examples: ``generic formal procedure'' or a ``formal integer type declaration.''
Dynamic Semantics
(10)
The elaboration of a generic_declaration has no effect.

(11)
(12)
(13)
Examples
(14)
Examples of generic formal parts:
(15)
       generic     --  parameterless
(16)
       generic
          Size : Natural;  --  formal object
(17)
       generic
          Length : Integer := 200;          -- formal object with a default expression
(18)
          Area   : Integer := Length*Length; -- formal object with a default expression
(19)
       generic
          type Item  is private;                       -- formal type
          type Index is (<>);                          -- formal type
          type Row   is array(Index range <>) of Item; -- formal type
          with function "<"(X, Y : Item) return Boolean;    -- formal subprogram
(20)
Examples of generic declarations declaring generic subprograms Exchange and Squaring:
(21)
       generic
          type Elem is private;
       procedure Exchange(U, V : in out Elem);
(22)
       generic
          type Item is private;
          with function "*"(U, V : Item) return Item is <>;
       function Squaring(X : Item) return Item;
(23)
Example of a generic declaration declaring a generic package:
(24)
       generic
          type Item   is private;
          type Vector is array (Positive range <>) of Item;
          with function Sum(X, Y : Item) return Item;
       package On_Vectors is
          function Sum  (A, B : Vector) return Vector;
          function Sigma(A    : Vector) return Item;
          Length_Error : exception;
       end On_Vectors;


____ | Up | Next | Back | Forward
TOC -- / --.-- / --.--.-- | Index | Search | Syntax | Help

Ada WWW Home -- Email comments, additions, corrections, gripes, kudos, etc. to:

Magnus Kempe -- Magnus.Kempe@di.epfl.ch
Copyright statement
Page last generated: 95-03-12