-*- Mode: Text; Fonts: Ct18b -*- 13.2 PACKAGES AND PRIVATE TYPES Earlier, we mentioned that packages can enforce the principle of abstraction. Often, a programmer wishes to create an object whose logical properties must be maintained outside a package but whose structural details are irrelivent. This is accomplished primarily with private types. A private type definition may appear only in the visible part of a package. There exist two cases of private types: --simple private types and limited private types. For simple private types, the only information available outside the package is that given in the visible part of the package in which it was declared. Thus, the type name is available, but the set of values or structure applicable to the type is hidden. The only operations available to objects of the private type are those applicable subprograms declared in the visible part, plus the assignment operator and tests for equality and inequality. For limited private types, the same rules apply, except that even assignment and tests for equality and in- equality are not available outside the package. Of course, within the private part and body of the same package, the structure of the private type is visible, which means that its structure may be referenced. If a package contains a private type definition, then the specification must also contain a private part that completes the type definition. A private part may contain more than the definition of the private type (although this is generally not good style). Futhermore, a package without a private type may include a private part. Within a package specification, we may not only define private types but may also declare constants of a private type (although we may not define objects of the private type until the elaboration of the private part). For example, we may wish to create a package that issues PASSWORDs and also checks their validity. As we create PASSWORD objects, we want to initialize them to some NULL_PASSWORD value, which we can declare as a private constant: package MANAGER is type PASSWORD is private; NULL_PASSWORD : constant PASSWORD; function GET return PASSWORD; function IS_VALID (P : in PASSWORD) return BOOLEAN; private type PASSWORD is range 0..7_000; NULL_PASSWORD : constant PASSWORD :=0; end MANAGER; We have chosen to declare PASSWORD as a private type, as opposed to limited private, to permit users to assign PASSWORDs to each other. How- ever, we cannot directly name values of the type, since the complete type declaration is hidden from us. Even though we have hidden the implementation of the constant, we may freely use NULL_PASSWORD outside the package. For example, when declaring PASSWORD objects, we can indicate a default value: use MANAGER; MY_PASSWORD : PASSWORD :=NULL_PASSWORD; Private and limited private types therefore permit a programmer to exercise complete control over the operations available on an exported type. This feature is particularly useful when creating abstract data types, which we discuss in the next section.