[Home] [Prev] [Next] [Index]

7.3 Package Bodies

7.3 Package Bodies

1
In contrast to the entities declared in the visible part of a package specification, the entities declared in the package body are only visible within the package body itself. As a consequence, a package with a package body can be used for the construction of a group of related subprograms (a package in the usual sense), in which the logical operations available to the users are clearly isolated from the internal entities.

2
For the elaboration of a package body, its declarative part is first elaborated, and its sequence of statements (if any) is then executed. The optional exception handlers at the end of a package body service exceptions raised during the execution of the sequence of statements of the package body.

Notes:

3
A variable declared in the body of a package is only visible within this body and, consequently, its value can only be changed within the package body. In the absence of local tasks, the value of such a variable remains unchanged between calls issued from outside the package to subprograms declared in the visible part. The properties of such a variable are similar to those of an "own" variable of Algol 60.

4
The elaboration of the body of a subprogram declared in the visible part of a package is caused by the elaboration of the body of the package. Hence a call of such a subprogram by an outside program unit raises the exception PROGRAM_ERROR if the call takes place before the elaboration of the package body (see 3.9).

5
Example of a package:

package RATIONAL_NUMBERS is

      type RATIONAL is
      record
            NUMERATOR : INTEGER;
            DENOMINATOR : POSITIVE;
      end record;

      function EQUAL(X,Y : RATIONAL) return BOOLEAN;

      function "/" (X,Y : INTEGER) return RATIONAL;
            -- to construct a rational number

      function "+" (X,Y : RATIONAL) return RATIONAL;
      function "-" (X,Y : RATIONAL) return RATIONAL;
      function "*" (X,Y : RATIONAL) return RATIONAL;
      function "/" (X,Y : RATIONAL) return RATIONAL;
end;

package body RATIONAL_NUMBERS is

      procedure SAME_DENOMINATOR (X,Y : in out RATIONAL) is
      begin
            -- reduces X and Y to the same denominator:
            ...
      end;

      function EQUAL(X,Y : RATIONAL) return BOOLEAN is
            U,V : RATIONAL;
      begin
            U := X;
            V := Y;
            SAME_DENOMINATOR (U,V);
            return U.NUMERATOR = V.NUMERATOR;
      end EQUAL;

      function "/" (X,Y : INTEGER) return RATIONAL is
      begin
            if Y > 0 then
                  return (NUMERATOR => X, DENOMINATOR => Y);
            else
                  return (NUMERATOR => -X, DENOMINATOR => -Y);
            end if;
      end "/";

      function "+" (X,Y : RATIONAL) return RATIONAL is ... end "+";
      function "-" (X,Y : RATIONAL) return RATIONAL is ... end "-";
      function "*" (X,Y : RATIONAL) return RATIONAL is ... end "*";
      function "/" (X,Y : RATIONAL) return RATIONAL is ... end "/";

end RATIONAL_NUMBERS;

6
References:

*
declaration 3.1

*
declarative part 3.9

*
elaboration 3.1

*
elaboration 3.9

*
exception 11

*
exception handler 11.2

*
name 4.1

*
package specification 7.1

*
program unit 6

*
program_error exception 11.1

*
sequence of statements 5.1

*
subprogram 6

*
variable 3.2.1

*
visible part 7.2



[Home] [Prev] [Next] [Index]

documentation@rational.com
Copyright © 1993-2000, Rational Software Corporation. All rights reserved.