Previous Table of Contents Next


10.4.1.1. Types and Objects

A type consists of two sets: a set of values and a set of operations appropriate for those values. Each variable and constant in Ada has a type; variables and constants are thus holders for values. The Ada standard defines a number of standard types either in the language—for example, Integer, Float, Boolean, Duration (elapsed time), Character, and String—or in standard library packages—for example, Time and Complex. Further, the programmer can define application-specific types of various kinds (for example, enumerations, records, arrays, and tasks). Conversion among types can be done with explicit type-conversion operations; implicit type conversion does not occur in Ada.

The Ada standard requires that the compiler ensure that operations are appropriate for the values to which they are applied. Appropriateness is checked statically at compilation time, wherever possible; otherwise, runtime checks are compiled into the executable program. If a runtime check fails (for example, the program attempts to store a computed negative value in a variable declared as nonnegative), a standard exception is raised (in this case, Constraint_Error). A raised exception can be handled by the program if code has been provided to do so; otherwise, the program terminates.

Because in Ada, each variable or constant has a state—its value—and a well-defined behavior—the set of operations defined for its type, it is entirely appropriate to refer to variables and constants as objects, and I shall do so in this development.

10.4.1.2. Packages and Files

A package encapsulates a collection of resources: type declarations, functions, procedures, variables, and constants. A package generally consists of two compilation units, which developers generally choose to place in distinct source files. The package specification or interface unit gives a list of those resources intended to be directly visible to clients; the body or implementation unit contains the code bodies of the procedures and functions, as well as any internal resources used by the packages but not exported to the client.

A package interface is roughly analogous to a “header file” as used in other languages but differs in that it is required and its contents are strictly structured. The package interface can be seen as a required “contract” between a package’s developer and its user.

In other languages—the C family in particular—source programs are organized into physical files. These files have semantic meaning in the language—they create scope and visibility—so the precise contents of each file are of critical importance. In Ada, this semantic content is embodied in the compilation unit.

Package interfaces, package implementations, and main procedures are all compilation units; whether the units making up a program are distributed into individual files or collected together in a single file, or something in between, is irrelevant to the program semantics, and is, in fact, never discussed in the standard. Some compilers have a preferred file structure, but this can be overridden. Thus the organization of a program into physical files is a matter of one’s organizational style and does not affect the program’s meaning.

The Ada standard libraries are implemented as packages; application programs are constructed from these and application-specific or generally reusable but non-standard packages. In this major section, the package examples all come from the standard libraries; in the next one, I introduce some packages of my own.

10.4.1.3. The Usual “Hello World” Program

The first example is Hello.

    1 with Ada.Text_IO;
    2 procedure Hello is
    3
    4   -- A first very small, obligatory program
    5
    6 begin -- Hello
    7
    8   Ada.Text_IO.Put_Line
    9     (Item => “This is the obligatory Hello program!”);
   10
   11 end Hello;

An Ada program usually consists of a main procedure, whose statements make reference to resources that are either locally declared or imported from packages. Each package is mentioned in a with clause—officially called a context clause—at the top of the program; in line 1 of Hello, the standard Ada input/output library Ada.Text_IO is indicated.

Lines 8 and 9 use the procedure Put_Line to display the quoted string on the standard output file and move to a new line. I have used the full form of the call: The package name Ada.Text_IO precedes the call and the name of the formal parameter Item is associated with its actual value, using the symbol =>. This may seem a bit verbose; I show some terser forms later.

The words with, procedure, is, begin, and end are five of Ada’s reserved words. These are used for various syntactic purposes and cannot be used in other contexts, such as variable names. By the end of this section, you will have seen most of the reserved words.

Put_Line, on the other hand, is the name of a standard procedure. Procedure names are not reserved; you could write another package that provided a procedure Put_Line and call this procedure in a program, as long as the compiler had enough information to resolve the potential ambiguity in the name.

The layout of a program in terms of lines is not specified by the language, nor is the case of reserved words and identifiers. A program is treated syntactically as a sequence of characters; indentation and use of blank lines and other whitespace is purely a matter of programmer style. The style I use here—lowercase reserved words, mixed-case identifiers, and whitespace—is generally consistent with the Reference Manual and most textbooks and with generally accepted practice.

The only exception regarding lines is that a comment—inserted for the human reader and ignored by the compiler—begins with -- and ends at the end of the same line. This line-oriented comment style—used in Fortran and assembler and adopted by C++—was chosen in preference to the bracketed style of Pascal and C to make it obvious that a block of code has been “commented out.” Lines 4 and 6 show comments used in two ways: an entire line and following some Ada code.

This section’s examples do not include space-taking program comments but concentrate on numbered lines and prose explanations.


Previous Table of Contents Next