When declaring a type in a package declaration, you can declare the type as private, and then complete the definition in a section of the package declaration in a section called the private part. If a type is declared as private, other packages can only use the operations that you provide and the default assignment (:=) and equality (=) operations.
Here's an example. Let's say that you want to create a type called `Key', which uniquely identifies some resource; you only want people to be able to Request a key and determine if one key was requested before another (let's call that operation "<"). Here's one way to implement this (this example is inspired by the Ada LRM section 7.3.1):
package Key_Manager is type Key is private; Null_Key : constant Key; -- a deferred constant. procedure Get_Key(K : out Key); -- Get a new Key value. function "<"(X, Y : Key) return Boolean; -- True if X requested before Y private Max_Key : constant := 2 ** 16 - 1; type Key is range 0 .. Max_Key; Null_Key : constant Key := 0; end Key_Manager;Note that the type declaration in the package declaration is declared as `private'. This is later followed by the word `private' introducing the `private part' of the package specification. Here the type can be defined, as well as any constants necessary to complete its definition. Although Key is actually a numeric type, other packages cannot use addition, multiplication, and other numeric operations because Key is declared as `private' - the only operations are those defined in the package (and := and =).
What if we don't want the default assignment (:=) and equals-to (=) operations? The answer: we declare the type as limited. A limited type doesn't have the default assignment and equals-to operations defined. Usually the term limited is combined with private, and such a type is called a limited private type. Here's how Key would be defined using the limited keyword:
type Key is limited private;
Go back to the previous section
Go up to the outline of lesson 6
David A. Wheeler (wheeler@ida.org)