Instructor's Manual for
M.B. Feldman and E.B. Koffman, Ada 95 Problem Solving and Program Design, Copyright 1996, Addison-Wesley Publishing Company. All Rights Reserved.
Questions and comments to mfeldman@seas.gwu.edu.
last revised October 1996
The student will
unconstrained array type |
the "box" symbol |
generic unit |
generic type parameter |
generic subprogram parameter |
instantiation |
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.
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.
The case study shows the utility of unconstrained array types by sorting different slices of an array.
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.)
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.
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.