Previous Table of Contents Next


A note on context clauses: A context clause is only superficially similar to the #include of C and other languages. It serves two purposes:

  At compilation time, the clause provides context for the compilation: The compiler reads each package interface (in source or intermediate form, depending on the compiler) and checks the correctness of each reference made to resources in that package.
  At binding and linking time, the clause is used to bind the compiled form of the main with the compiled form of the packages; the binder and linker must ensure that the compiled form of each package is up to date.

There is no exact equivalent in Ada to the #include, which simply copies the contents of another file. One could in theory use a preprocessor to get the equivalent, but it is almost never necessary to do so because context clauses provide similar functionality with more integrity-checking.

A final point on the standard libraries: The many Ada 95 standard libraries are easy to recognize. Their names all begin with Ada. They are in fact child packages of the root package Ada. More on child packages later.

10.4.1.4. A Less Verbose Coding Style

Hello_Terse shows two ways of reducing the “verbosity” of a program. As you shall see, one programmer’s verbosity may well be another’s clarity.

    1 with Ada.Text_IO;
    2 use  Ada.Text_IO;
    3 procedure Hello_Terse is
    4 -- A terse version of the Hello program
    5 begin
    6   Put_Line (“This is the obligatory Hello program!”);
    7 end Hello_Terse;

First, I add a use clause (line 2), which makes all the resources of the given package directly visible. That is, you can name a resource (as in line 6) without preceding the reference with the package name. This may seem like an obvious advantage, but for longer programs using a number of packages, omitting package names tends to obscure the meaning of the statements and to lead to reader confusion. I return to this issue later in the chapter.

Second, I omit the formal parameter name. This, too, may seem like an obvious advantage, but for procedures with a number of parameters, using the named association allows you to supply actual parameters in any order and may also lead to clearer, less error-prone procedure calls.

10.4.1.5. The Calendar Package

The next several examples examine the standard library package, Ada.Calendar, and use it while introducing several Ada data and control structures.

Here is an extract from the package interface of Ada.Calendar, which provides date and time services in a standard fashion. The resources listed in the interface are exported to client programs.

    1 package Ada.Calendar is
    2   type Time is private;
    3
    4   subtype Year_Number  is Integer range 1901..2099;
    5   subtype Month_Number is Integer range 1..12;
    6   subtype Day_Number   is Integer range 1..31;
    7   subtype Day_Duration is Duration range 0.0..86_400.0;
    8
    9   function Clock return Time;
   10
   11   function Year   (Date : Time) return Year_Number;
   12   function Month  (Date : Time) return Month_Number;
   13   function Day    (Date : Time) return Day_Number;
   14   function Seconds(Date : Time) return Day_Duration;
   15    ...
   16 end Ada.Calendar;

Time is declared (line 2) as a private type, which signals that its structure—its set of values—is not directly available to me. I use time values only via package operations.

Lines 4-7 define four subtypes. A subtype declaration creates a subset of the values of the original type; subtypes are commonly used, as here, to restrict the appropriate ranges of certain variables. The three Integer subtypes here have obvious meanings in the calendar; the three functions in lines 11-13 extract the corresponding components from a Time value. The Duration subtype Day_Duration provides the elapsed time values in a 24-hour period; the function Seconds returns the time elapsed since midnight of the given day.


Previous Table of Contents Next