Instructor's Manual for

M.B. Feldman and E.B. Koffman, Ada 95 Problem Solving and Program Design, 3rd edition. Copyright 1999, Addison-Wesley Publishing Company. All Rights Reserved.

Questions and comments to mfeldman@seas.gwu.edu.


Chapter 12 Introduction to Unconstrained Types and Generics

last revised March 1999

Chapter Objectives

The student will
  1. learn to declare and use unconstrained array types and their attribute functions
  2. learn to write general array-handling subprograms that do not need to know the bounds of their parameters at compilation time
  3. be introduced to generic functions and procedures
  4. learn how to write a generic sorting routine
  5. learn how to write a generic set package

New Terms

Section 12.1

unconstrained array type the "box" symbol

Section 12.3

generic unit generic type parameter
generic subprogram parameter instantiation

Notes and Suggestions

General Suggestions

Unconstrained array types and generic units are two of Ada's most powerful features for writing general-purpose library packages. Unconstrained array types allow subprograms to be written with array parameters whose bounds are unknown at compilation time; attribute functions give the subprogram the ability to obtain the bounds when the subprogram is called. Generic units are units defined with generic parameters--types and subprograms. A developer can write a generic unit which operates on any of a class of types, the actual type to be supplied at instantiation. It is often necessary to pass the names of operations on the actual type, and so generic subprogram parameters are defined. Students reaching this material in a course will come away knowing a fair amount about how to write some very powerful programs.

Section 12.1

Unconstrained array types are really quite straightforward, and are appreciated by students with experience in languages such as Pascal and C whose array facilities are quite limited. They might wish to know whether slicing is available for multidimensional arrays; it is not. The Ada designers opted not to allow it out of concern for implementation efficiency; programmers in PL/1 often used that language's powerful array slicing features to write code that was elegant but horribly slow.

Section 12.2

The case study shows the utility of unconstrained array types by sorting different slices of an array.

Section 12.3

The most difficult part of generics is the strange and unexpected new meaning given to familiar concepts. You will need to explain carefully that generic type parameters do not declare a type, even though they look like type declarations. A generic type parameter is a template for a class of types.

For example, IS PRIVATE is a template for the class of all types for which assignment and equality test are defined, i.e. all non-LIMITED PRIVATE types. You can rationalize the strange use of PRIVATE by saying that it allows all types, including PRIVATE ones, but not LIMITED PRIVATE ones, to be supplied as actual parameters.

The idea that a template for a subprogram can be written, allowing the name of a subprogram to be passed as a parameter, may be strange to your students unless they have studied recent versions of C++. On the other hand, those with experience in languages that allow function and procedure parameters (FORTRAN, Pascal, C), may notice that generic subprogram parameters accomplish something similar through instantiation. (Ada 83 did not allow function or procedure parameters in the FORTRAN or Pascal sense; this is added in Ada 95 but we do not cover it in this text.)

Section 12.4

This section shows the power of generics by illustrating, in a single program, instances of a generic sort for both upward and downward sorting of arrays with various structures. Take the time to go through it carefully with your students.

Section 12.5

This section is a good exercise in generics, especially for those students who can relate to the mathematical concepts. The set package developed here, in which sets are represented by "bit maps" is an approximation to the predefined sets of Pascal.