Concepts of Programming Languages

(Sean Pound) #1
11.4 Language Examples 483

specification (not the body package), but the compiler must be able to allocate
objects of the exported type when compiling the client. Furthermore, the client
is compilable when only the package specification for the abstract data type has
been compiled and is present. Therefore, the compiler must be able to deter-
mine the size of an object from the package specification. So, the representation
of the type must be visible to the compiler but not to the client code. This is
exactly the situation specified by the private clause in a package specification.
An alternative to private types is a more restricted form: limited private
types. Nonpointer limited private types are described in the private section
of a package specification, as are nonpointer private types. The only syntactic
difference is that limited private types are declared to be limited private
in the visible part of the package specification. The semantic difference is that
objects of a type that is declared limited private have no built-in operations.
Such a type is useful when the usual predefined operations of assignment and
comparison are not meaningful or useful. For example, assignment and com-
parison are rarely used for stacks.


11.4.1.3 An Example


The following is the package specification for a stack abstract data type:


package Stack_Pack is
-- The visible entities, or public interface
type Stack_Type is limited private;
Max_Size : constant := 100;
function Empty(Stk : in Stack_Type) return Boolean;
procedure Push(Stk : in out Stack_Type;
Element : in Integer);
procedure Pop(Stk : in out Stack_Type);
function Top(Stk : in Stack_Type) return Integer;
-- The part that is hidden from clients
private
type List_Type is array (1..Max_Size) of Integer;
type Stack_Type is
record
List : List_Type;
Topsub : Integer range 0..Max_Size := 0;
end record;
end Stack_Pack;


Notice that no create or destroy operations are included, because they are not
necessary.
The body package for Stack_Pack is as follows:


with Ada.Text_IO; use Ada.Text_IO;
package body Stack_Pack is

Free download pdf