Ada 95 Quality and Style Guide Chapter 5

Chapter 5: Programming Practices - TOC - 5.3 TYPES

5.3.4 Subprogram Access Types


  • Use access-to-subprogram types for indirect access to subprograms.
  • Wherever possible, use abstract tagged types and dispatching rather than access-to-subprogram types to implement dynamic selection and invocation of subprograms.

  • example

    The following example is taken from the Rationale (1995, §3.7.2):

       type Float_Type is digits <>;
    package Generic_Integration is
       type Integrand is access function (X : Float_Type) return Float_Type;
       function Integrate (F        : Integrand;
                           From     : Float_Type;
                           To       : Float_Type;
                           Accuracy : Float_Type := 10.0*Float_Type'Model_Epsilon)
         return Float_Type;
    end Generic_Integration;
    with Generic_Integration;
    procedure Try_Estimate (External_Data : in     Data_Type;
                            Lower         : in     Float;
                            Upper         : in     Float;
                            Answer        :    out Float) is
       -- external data set by other means
       function Residue (X : Float) return Float is
          Result : Float;
       begin  -- Residue
          -- compute function value dependent upon external data
          return Result;
       end Residue;
       package Float_Integration is
          new Generic_Integration (Float_Type => Float);
       use Float_Integration;
    begin -- Try_Estimate
       Answer := Integrate (F    => Residue'Access,
                            From => Lower,
                            To   => Upper);
    end Try_Estimate;


    Access-to-subprogram types allow you to create data structures that contain subprogram references. There are many uses for this feature, for instance, implementing state machines, call backs in the X Window System, iterators (the operation to be applied to each element of a list), and numerical algorithms (e.g., integration function) (Rationale 1995, §3.7.2).

    You can achieve the same effect as access-to-subprogram types for dynamic selection by using abstract tagged types. You declare an abstract type with one abstract operation and then use an access-to-class-wide type to get the dispatching effect. This technique provides greater flexibility and type safety than
    access-to-subprogram types (Ada Reference Manual 1995, §3.10.2).

    Access-to-subprogram types are useful in implementing dynamic selection. References to the subprograms can be stored directly in the data structure. In a finite state machine, for example, a single data structure can describe the action to be taken on state transitions. Strong type checking is maintained because Ada 95 requires that the designated subprogram has the same parameter/result profile as the one specified in the subprogram access type.

    See also Guideline 7.3.2.

    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    Appendix References Bibliography