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


7.2 Package Bodies

(1)
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
(2)
       package_body ::=
           package body defining_program_unit_name is
              declarative_part
          [begin
               handled_sequence_of_statements]
           end [[parent_unit_name.]identifier];
(3)
Legality Rules
(4)
A package_body shall be the completion of a previous package_declaration or generic_package_declaration. A library package_declaration or library generic_package_declaration shall not have a body unless it requires a body; pragma Elaborate_Body can be used to require a library_unit_declaration to have a body (see 10.2.1) if it would not otherwise require one.
Static Semantics
(5)
In any package_body without statements there is an implicit null_statement. For any package_declaration without an explicit completion, there is an implicit package_body containing a single null_statement. For a noninstance, nonlibrary package, this body occurs at the end of the declarative_part of the innermost enclosing program unit or block_statement; if there are several such packages, the order of the implicit package_bodies is unspecified. (For an instance, the implicit package_body occurs at the place of the instantiation (see 12.3). For a library package, the place is partially determined by the elaboration dependences (see Section 10).)
Dynamic Semantics
(6)
For the elaboration of a nongeneric package_body, its declarative_part is first elaborated, and its handled_sequence_of_statements is then executed.

(7)
(8)
Examples
(9)
Example of a package body (see 7.1):
(10)
       package body Rational_Numbers is
(11)
          procedure Same_Denominator (X,Y : in out Rational) is
          begin
             --  reduces X and Y to the same denominator:
             ...
          end Same_Denominator;
(12)
          function "="(X,Y : Rational) return Boolean is
             U : Rational := X;
             V : Rational := Y;
          begin
             Same_Denominator (U,V);
             return U.Numerator = V.Numerator;
          end "=";
(13)
          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 "/";
(14)
          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 "/";
(15)
       end Rational_Numbers;


Prev | 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