7.2 Package Bodies
[In contrast to the entities declared in the visible
part of a package, the entities declared in the
package_body
are visible only 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 in
which the logical operations available to clients are clearly isolated
from the internal entities.]
Syntax
Legality Rules
Static Semantics
Discussion: Thus, for example, we can
refer to something happening just after the
begin of a
package_body,
and we can refer to the
handled_sequence_of_statements
of a
package_body,
without worrying about all the optional pieces. The place of the implicit
body makes a difference for tasks activated by the package. See also
RM83-9.3(5).
The implicit body would be illegal if explicit
in the case of a library package that does not require (and therefore
does not allow) a body. This is a bit strange, but not harmful.
Dynamic Semantics
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 a “static” variable of C.
4 The elaboration of the body of a subprogram
explicitly 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.11).
Examples
Example of a package
body (see 7.1):
package body Rational_Numbers is
procedure Same_Denominator (X,Y : in out Rational) is
begin
-- reduces X and Y to the same denominator:
...
end Same_Denominator;
function "="(X,Y : Rational) return Boolean is
U : Rational := X;
V : Rational := Y;
begin
Same_Denominator (U,V);
return U.Numerator = V.Numerator;
end "=";
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;
Wording Changes from Ada 83
RM83 seems to have forgotten to say that a
package_body
can't stand alone, without a previous declaration. We state that rule
here.
RM83 forgot to restrict the definition of elaboration
of package_bodies to nongeneric ones. We have
corrected that omission.
The rule about implicit bodies (from RM83-9.3(5))
is moved here, since it is more generally applicable.
Extensions to Ada 2005
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe