Lady Ada

Ada '83 Language Reference Manual

Copyright 1980, 1982, 1983 owned by the United States Government. Direct reproduction and usage requests to the Ada Information Clearinghouse.

12.2. Generic Bodies


The body of a generic subprogram or generic package is a template for the bodies of the corresponding subprograms or packages obtained by generic instantiations. The syntax of a generic body is identical to that of a nongeneric body.

For each declaration of a generic subprogram, there must be a corresponding body.

The elaboration of a generic body has no other effect than to establish that the body can from then on be used as the template for obtaining the corresponding instances.

Example of a generic procedure body:

    procedure EXCHANGE(U, V : in out ELEM) is  --  see example in 12.1
       T : ELEM;  --  the generic formal type
       T := U;
       U := V;
       V := T;
    end EXCHANGE;  

Example of a generic function body:

    function SQUARING(X : ITEM) return ITEM is  --  see example in 12.1
       return X*X;  --  the formal operator "*"

Example of a generic package body:

    package body ON_VECTORS is  --  see example in 12.1 

       function SUM(A, B : VECTOR) return VECTOR is
          RESULT : VECTOR(A'RANGE);     --  the formal type VECTOR
          BIAS   : constant INTEGER := B'FIRST - A'FIRST;
          if A'LENGTH /= B'LENGTH then
             raise LENGTH_ERROR;
          end if; 

          for N in A'RANGE loop
             RESULT(N) := SUM(A(N), B(N + BIAS));  -- the formal function SUM
          end loop;
          return RESULT;

       function SIGMA(A : VECTOR) return ITEM is
          TOTAL : ITEM := A(A'FIRST);                --  the formal type ITEM
          for N in A'FIRST + 1 .. A'LAST loop
             TOTAL := SUM(TOTAL, A(N));           --  the formal function SUM
          end loop;
          return TOTAL;

References: body, elaboration, generic body, generic instantiation, generic package, generic subprogram, instance, package body, package, subprogram, subprogram body.

Rationale references: 12.3 The Use of Generic Units, 12.3.1 Examples of Generic Functions, 12.3.2 An Example of a Generic Package, 12.3.3 A Generic Package with Tasks, 12.3.4 A More Complicated Example

Style Guide references: 8.2.7 Exceptions


[Ada Information Clearinghouse]

Address any questions or comments to