12 - Packages and Private Types
Published online by Cambridge University Press: 05 July 2014
Summary
The previous chapters described the small-scale features of Ada in some detail. These features correspond to the areas addressed by the pioneering languages prior to about 1975. This chapter and the next discuss the important concepts of abstraction and programming in the large which entered into languages generally around 1980. The more recent concepts of type extension, inheritance and polymorphism are dealt with in Chapter 14.
In this chapter we discuss packages (which is really what Ada is all about) and the important concept of a private type. The topic of library units and especially library packages is discussed in detail in the next chapter.
Packages
In this section we introduce the concept of an Abstract State Machine (ASM) and in the next section we extend this to Abstract Data Types (ADT). The general idea of abstraction is to distinguish the inner details of how something works from an external view of it. This is a common idea in everyday life. We can use a watch to look at the time without needing to know how it works. Indeed the case of the watch hides its inner workings from us.
One of the major problems with early simple languages, such as C and Pascal, is that they do not offer enough control of visibility. For example, suppose we have a stack implemented as an array plus a variable to index the current top element, thus
Max: constant := 100;
S: array (1 .. Max) of Integer;
Top: Integer range 0 .. Max;
We might then declare a procedure Push to add an item and a function Pop to remove an item.
procedure Push(X: in Integer) is
begin
Top := Top + 1;
S(Top) := X;
end Push;
function Pop return Integer is
begin
Top := Top - 1;
return S(Top + 1);
end Pop;
In a simple block structured language it is not possible to have access to the subprograms Push and Pop without also having direct access to the variables S and Top. As a result we can bypass the intended protocol of only accessing the stack through calls of Push and Pop. For example we might make use of our knowledge of the implementation details and change the value of Top directly without corresponding changes to the array S.
- Type
- Chapter
- Information
- Programming in Ada 2012 , pp. 229 - 262Publisher: Cambridge University PressPrint publication year: 2014