$33.2 PACKAGES 1081 critiques of Ada techniques should be viewed less as reproach than as homage to the precursor against which any new solution must naturally be assessed. 33.2 PACKAGES See“Packages", Each of the encapsulation languages offers a modular construct for grouping logically page 90. related program elements.Ada calls it a package;corresponding notions are known as modules in Modula-2 and Mesa,and clusters in CLU. “Modules and A class was defined as both a structural system component-a module-and a pes”,page170. type.In contrast,a package is only a module.An earlier discussion described this difference by noting that packages are a purely syntactic notion,whereas classes also have a semantic value.Packages provide a way to distribute system elements (variables, routines ...into coherent subsystems;but they are only needed for readability and manageability of the software.The decomposition of a system into packages does not affect its semantics:one can transform a multi-package Ada system into a one-package system,producing exactly the same results,through a purely syntactical operation- removing all package boundaries,expanding generic derivations(as explained below)and resolving name clashes through renaming.Classes,for their part,are also a semantic construct:besides providing a unit of modular decomposition,a class describes the behavior of a set of run-time objects;this semantics is further enriched by polymorphism and dynamic binding. An Ada package is a free association of program elements and may be used for various purposes.Sensible uses of this notion include writing a package to gather: "Facility inherit- A set of related constants (as with facility inheritance). ance",page 832. A library of routines,for example a mathematical library. A set of variables,constants and routines describing the implementation of one abstract object,or a fixed number of abstract objects,accessible only through designated operations(as we will do in Fortran in the next chapter). An abstract data type implementation. The last use is the most interesting for this discussion.We will study it through the example of a stack package,adapted from an example in the Ada reference manual. 33.3 A STACK IMPLEMENTATION Information hiding is supported in Ada by the two-tier declaration of packages.Every package comes in two parts,officially called"specification"and"body".The former term is too strong for a construct that does not support any formal description of package semantics (in the form of assertions or similar mechanisms),so we will use the more The standard Ada term modest word“interface”. for"routine”is“"sub. program”.We keep the The interface lists the public properties of the package:exported variables,constants, former for consistency types and routines.For routines it only gives the headers,listing the formal arguments and with other chapters. their types,plus the result type for a function,as in:§33.2 PACKAGES 1081 critiques of Ada techniques should be viewed less as reproach than as homage to the precursor against which any new solution must naturally be assessed. 33.2 PACKAGES Each of the encapsulation languages offers a modular construct for grouping logically related program elements. Ada calls it a package; corresponding notions are known as modules in Modula-2 and Mesa, and clusters in CLU. A class was defined as both a structural system component — a module — and a type. In contrast, a package is only a module. An earlier discussion described this difference by noting that packages are a purely syntactic notion, whereas classes also have a semantic value. Packages provide a way to distribute system elements (variables, routines …) into coherent subsystems; but they are only needed for readability and manageability of the software. The decomposition of a system into packages does not affect its semantics: one can transform a multi-package Ada system into a one-package system, producing exactly the same results, through a purely syntactical operation — removing all package boundaries, expanding generic derivations (as explained below) and resolving name clashes through renaming. Classes, for their part, are also a semantic construct: besides providing a unit of modular decomposition, a class describes the behavior of a set of run-time objects; this semantics is further enriched by polymorphism and dynamic binding. An Ada package is a free association of program elements and may be used for various purposes. Sensible uses of this notion include writing a package to gather: • A set of related constants (as with facility inheritance). • A library of routines, for example a mathematical library. • A set of variables, constants and routines describing the implementation of one abstract object, or a fixed number of abstract objects, accessible only through designated operations (as we will do in Fortran in the next chapter). • An abstract data type implementation. The last use is the most interesting for this discussion. We will study it through the example of a stack package, adapted from an example in the Ada reference manual. 33.3 A STACK IMPLEMENTATION Information hiding is supported in Ada by the two-tier declaration of packages. Every package comes in two parts, officially called “specification” and “body”. The former term is too strong for a construct that does not support any formal description of package semantics (in the form of assertions or similar mechanisms), so we will use the more modest word “interface”. The interface lists the public properties of the package: exported variables, constants, types and routines. For routines it only gives the headers, listing the formal arguments and their types, plus the result type for a function, as in: See “Packages”, page 90. “Modules and types”, page 170. “Facility inheritance”, page 832. The standard Ada term for “routine” is “subprogram”. We keep the former for consistency with other chapters