Next: , Previous: 12.1, Up: 12


12.2 Generic Bodies

1
The body of a generic unit (a generic body) is a template for the instance bodies. The syntax of a generic body is identical to that of a nongeneric body.

Dynamic Semantics

2
The elaboration of a generic body has no other effect than to establish that the generic unit can from then on be instantiated without failing the Elaboration_Check. If the generic body is a child of a generic package, then its elaboration establishes that each corresponding declaration nested in an instance of the parent (see 10.1.1) can from then on be instantiated without failing the Elaboration_Check.

     NOTES

3

 The syntax of generic subprograms implies that a generic subprogram body is always the completion of a declaration.
Examples

4
Example of a generic procedure body:

5

     procedure Exchange(U, in out Elem) is  −− see 12.1
        Elem;  −−  the generic formal type
     begin
        := U;
        := V;
        := T;
     end Exchange;

6
Example of a generic function body:

7

     function Squaring(X Item) return Item is  −−  see 12.1
     begin
        return X*X;  −−  the formal operator "*"
     end Squaring;

8
Example of a generic package body:

9

     package body On_Vectors is  −−  see 12.1

10

        function Sum(A, Vector) return Vector is
           Result Vector(A'Range); −−  the formal type Vector
           Bias   constant Integer := B'First − A'First;
        begin
           if A'Length /= B'Length then
              raise Length_Error;
           end if;

11

           for in A'Range loop
              Result(N) := Sum(A(N), B(N Bias)); −− the formal function Sum
           end loop;
           return Result;
        end Sum;

12

        function Sigma(A Vector) return Item is
           Total Item := A(A'First); −−  the formal type Item
        begin
           for in A'First .. A'Last loop
              Total := Sum(Total, A(N)); −−  the formal function Sum
           end loop;
           return Total;
        end Sigma;
     end On_Vectors;