Previous | Next | Table of Contents | Index | Program List | Copyright

Chapter Review

In this chapter we studied two important concepts in building reusable software components. Unconstrained array types allow us to define array types such that the bounds of a given array are left unspecified until the array variable is declared. Unconstrained array types facilitate writing general-purpose subprograms that deal with arrays, such as vector operations and sort procedures.

Generic definition allows us to create templates, or recipes, for subprograms and packages. These templates allow us to leave such things as parameter types, sizes, and operations unspecified until instantiation time. Once a generic template is compiled, multiple versions of it, called instances, can then be created, each with a single statement. The availability of generic definition and instantiation gives us the potential for building large and powerful libraries of reusable software components with much less effort and with much greater maintainability. In this chapter we saw a number of useful generic components for exchanging values, finding the maximum, sorting, and vector handling.

New Ada Constructs in Chapter 11

The new Ada constructs introduced in this chapter are described in Table 11.1.

Table 11.1 Summary of New Ada Constructs

Unconstrained Array Types

SUBTYPE Weeks IS Positive RANGE 1..52;		Declares an array
SUBTYPE Rainfall IS Float RANGE 0.0..500.0;	type whose variables
TYPE RainTable IS				can be indexed by
  ARRAY (Weeks RANGE <>) OF Rainfall;		any subrange of Weeks

SecondQuarter: RainTable(14..26);		and a variable with 13 elements

Generic Specification

GENERIC

  TYPE ValueType IS PRIVATE;				Specifies a function
  TYPE IndexType IS (<>);				to find the location 
  TYPE ArrayType IS					of the "largest"value
    ARRAY(IndexType RANGE <>) OF ValueType;		in an array.
  WITH FUNCTION Compare(L,R: ValueType)
    RETURN Boolean;

FUNCTION IndexOfMax(A: ArrayType) RETURN IndexType;

Quick-Check Exercises

  1. Define an unconstrained array type.
  2. How many dimensions can an unconstrained array type have?
  3. Explain what is meant by a generic template.
  4. What is a generic type parameter? Give examples.
  5. What is a generic procedure or function parameter? Give examples.
  6. Given a generic parameter
    WITH FUNCTION Compare(L,R: ValueType) RETURN Boolean;
    

    explain why it is legal to match this with an operator "<" or ">" at instantiation.

Answers to Quick-Check Exercises

  1. An unconstrained array type is one in which the bounds of array variables are not fixed until the variables are declared.
  2. There is no language-defined limit on the number of dimensions; unconstrained array types are no different from other array types in this regard.
  3. A specification of a procedure, function, or package that must be instantiated before it can be used.
  4. A generic type parameter specifies which class of types is acceptable as a match in creating an instance. Examples are: any type that is not limited private, any discrete type, and any unconstrained array type with given index and element types.
  5. A generic procedure or function parameter indicates to the compiler that the name of a procedure or function with a matching parameter list will be supplied at instantiation.
  6. An operator is just a certain kind of function. Ada does not care whether the name of such a function is an operator symbol or an identifier, as long as there is a correct match of the parameters and result type of the function.

Review Questions

  1. Explain how unconstrained array types, and array attributes, facilitate creating general-purpose array-handling programs.
  2. One generic parameter form we did not discuss in the chapter is
    TYPE SomeParameterName IS LIMITED PRIVATE;
    

    which allows any type, even a LIMITED PRIVATE one, to be supplied as a match at instantiation. Suppose we used one of these type forms in a generic package specification. What limitations would this place on the kinds of statements that could appear in the body of the package?

Programming Projects

  1. In Section 8.10 we developed a function Search (Program 8.14) that looks through an array for a particular value. Revise Search to make it generic, and write a test program for several instances.
  2. Revise the case study of Section 8.10, in which an array of score records is read from a file, then sorted. Use the generic sort procedure from Section 11.5, instantiating it as suggested there.
  3. One generic parameter form we did not discuss in the chapter is
    TYPE SomeParameterName IS LIMITED PRIVATE;
    

    which allows any type, even a LIMITED PRIVATE one, to be supplied as a match at instantiation. Suppose we used one of these type forms in a generic package specification. What limitations would this place on the kinds of statements that could appear in the body of the package?

  4. Demonstrate Maximum_Array_Generic for some interesting instantiations.
  5. A useful function similar to Maximum_Array_Generic is one that finds the location of the "maximum" value in an array or slice, rather than the value itself. Write such a function as a generic, then write a generic sort program that uses it.
  6. A useful function similar to GenericArrayMaximum is one that finds the location of the "maximum" value in an array or slice, rather than the value itself. Write such a function as a generic, then write a generic sort program that uses it.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.